Practice
Solution
Step 1: Understand problem constraints
The stream consists of lowercase letters only, so a fixed-size frequency array of length 26 suffices.Step 2: Identify optimal approach
Using a frequency array plus a queue to track candidates for first unique character allows O(1) updates per character and O(n) total time.Final Answer:
Option C -> Option CQuick Check:
Frequency array + queue approach is standard for this problem [OK]
- Thinking brute force scanning is efficient enough
- Assuming DP applies here
- Using greedy without frequency tracking
nextGreaterElement([4,1,2], [1,3,4,2])?Solution
Step 1: Trace stack and map updates for nums2
Process nums2: push 1, then 3 > 1 so pop 1 -> map[1]=3; push 3; 4 > 3 pop 3 -> map[3]=4; push 4; 2 < 4 push 2. Stack ends with [4,2].Step 2: Assign -1 for remaining stack elements and build result
Pop 2 -> map[2]=-1; pop 4 -> map[4]=-1. Result for nums1=[4,1,2] is [map[4], map[1], map[2]] = [-1,3,-1].Final Answer:
Option B -> Option BQuick Check:
Matches expected output from code comments and manual trace [OK]
- Confusing order of popping leading to wrong map values
- Forgetting to assign -1 for elements with no next greater
- Mixing up indices when building the final result
Solution
Step 1: Identify time complexity
The algorithm scans the input string once, performing constant-time operations per character, so time is O(n).Step 2: Identify space complexity
Only a fixed number of variables are used (total, last_num, current_num, current_op), so space is O(1).Final Answer:
Option A -> Option AQuick Check:
Single pass and constant variables confirm O(n) time and O(1) space [OK]
- Assuming stack usage means O(n) space
- Confusing recursion stack with iteration
- Thinking sorting operators is needed
Solution
Step 1: Identify operations per token
Each token is processed once; operands are pushed, operators pop two operands and push one result.Step 2: Analyze stack operations
Stack push/pop are O(1), so total time is proportional to number of tokens n.Final Answer:
Option D -> Option DQuick Check:
No nested loops or recursion in iterative approach [OK]
- Assuming O(n^2) due to repeated stack scans
- Confusing recursion stack space with time
- Thinking sorting is involved
Solution
Step 1: Understand reuse operation
Reuse means re-enqueueing the last dequeued element, so it should be treated as a new push.Step 2: Choose efficient modification
Storing the last popped element separately and pushing it onto push_stack on reuse preserves order and amortized efficiency.Step 3: Analyze other options
After every pop, immediately push the popped element back onto pop_stack to allow reuse, effectively making pop a peek operation, breaks queue semantics by not removing element. Clearing both stacks and re-inserting all elements including the reused element is inefficient. Using a third stack to hold reused elements and merging it with push_stack during transfers adds unnecessary complexity.Final Answer:
Option D -> Option DQuick Check:
Reuse as push to push_stack keeps order and efficiency [OK]
- Trying to reuse by pushing onto pop_stack
- Rebuilding stacks on reuse causing inefficiency
