Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Is it still a stack?

ethanedge

Mar 20, 2024

Does this bit of code violate what it means to be a stack?

        StringBuilder sb = new StringBuilder(); // Create a StringBuilder to build the result string         for (char c : stack) { // Iterate through the characters in the stack             sb.append(c); // Append each character to the StringBuilder         }         return sb.toString(); // Convert the StringBuilder to a string and return the result

As it is iterating from bottom to top, it's no longer LIFO.

1

0

Comments
Comments
D
dlflann 2 years ago

Yes, they keep treating stacks like queues or deques.

On this page