Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: A browser keeps history so that 'back' and 'forward' work like the real buttons, and visiting a new page clears the forward history. Which structure is natural?
- A. A single queue
- B. A min-heap ordered by visit time
- C. Two stacks (back and forward), or one array with a current pointer that truncates on a new visit
- D. A hash map from URL to time
Correct answer: C. Two stacks (back and forward), or one array with a current pointer that truncates on a new visit
Back pops from the back stack and pushes onto forward; forward does the reverse; a new visit clears forward. All O(1). A queue gives FIFO order (wrong for 'back'), and a heap or map loses the navigation sequence.
More Coding Interview Problems questions
- Problem: For a stream of n = 10⁶ (user, timestamp) hits, answer 'how many hits in the last 300 seconds?' after each new hit (timestamps…
- Problem: Implement a set that supports insert, delete and getRandom (uniformly random element) all in average O(1). Which combination works?
- Problem: Support 'add number' and 'find a pair summing to t' where adds are 10⁵ and finds are rare, versus the opposite workload. Which…
- Problem: Map each word of a 10⁶-word document to its frequency, then print words in alphabetical order. Which choice gives O(n log n) with…
- Problem: Count the number of distinct islands by shape in a grid (shapes match only by translation). python def distinct(g): R, C =…
- Problem: Count subarrays with at most k distinct values, then use it to count subarrays with exactly k distinct values. python from…