Back and Forward Navigation for an In-App Browser
Last updated: November 29, 2026
Quick Overview
Design the history for an in-app browser with visit(url), back(steps) and forward(steps). Visiting a new page clears the forward history, and back or forward stop at the ends.
Meta
November 29, 20260
0
0 solved
Design the history for an in-app browser with visit(url), back(steps) and forward(steps). Visiting a new page clears the forward history, and back or forward stop at the ends.
Links opened from Facebook and Instagram load in an in-app browser, and this question models its navigation stack. Interviewers want a simple, correct structure with O(1) operations and careful handling of the forward history being discarded.
What the Interviewer Expects
- Clarify clamping when steps exceed available history
- Choose an array with a cursor instead of two stacks
- Make visit O(1) by overwriting instead of truncating
- Track the last valid forward index
- Walk through a sequence of calls to prove correctness
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you cap history at the last 50 pages?
- How would you persist history across app restarts?
- What if each tab has its own history and tabs can be duplicated?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
visit(url)moves to a new page and clears forward history.back(k)andforward(k)move up toksteps and return the current url.- Start on a homepage....
High-Level Design
Two stacks work but cost O(k) per move. An array with a cur cursor and a last marker is simpler: back and forward just move the cursor within [0, last]. On visit we advance cur, write the url ...