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?

  1. A. A single queue
  2. B. A min-heap ordered by visit time
  3. C. Two stacks (back and forward), or one array with a current pointer that truncates on a new visit
  4. 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

Exams that ask this topic