Reverse a String Using a Stack

viaGlassdoor

Problem: Reverse a given string using a stack data structure explicitly (not built-in reverse functions).

Constraints: O(n) time, O(n) extra space for the stack.

Example: Input: 'hello' Output: 'olleh'

Approach: Push every character of the string onto a stack, then pop characters one by one (LIFO order naturally reverses the sequence) and append each popped character to a result string/array until the stack is empty.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account