Single-Tab Browser Navigation History

Quick Overview

Single-Tab Browser Navigation History. Implement BrowserHistory with the native constructor and methods.

Single-Tab Browser Navigation History

Company: Chime

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Implement BrowserHistory with the native constructor and methods. Begin at homepage. visit(url) goes to a new page and clears every forward-history page. back(steps) and forward(steps) move as far as available, at most steps,and return the resulting current URL. ```cpp class BrowserHistory { public: BrowserHistory(string homepage); void visit(string url); string back(int steps); string forward(int steps); }; ``` ### Examples **Example 1:** ```text Calls: BrowserHistory("home"); visit("a"); visit("b"); back(1); visit("c"); forward(2); back(9) Returned navigation values: ["a","c","home"] Explanation: Visiting c from a discards b; forward stays at c,and the large back request stops at home. ``` **Example 2:** ```text Calls: BrowserHistory("start"); back(1); visit("next"); back(1); forward(1) Returned navigation values: ["start","start","next"] Explanation: Back cannot leave the initial page, while the later back/forward pair traverses the one stored visit. ``` ### Constraints - homepage and each url have length 1 to 20 and consist only of lowercase English letters or periods. - `1 <= steps <= 100`. - At most 5000 calls in total are made to visit,back,and forward. ### Contract considerations Use the constructor, `visit`, `back`, and `forward` methods shown above; successive calls share the browsing history. ```hint Read the boundary condition Check navigation beyond available history and a new visit after going back. Calls share one tab's evolving history. ``` ### Boundary reminders Use the constructor, `visit`, `back`, and `forward` methods shown above; successive calls share the browsing history.

Overview: Single-Tab Browser Navigation History. Implement BrowserHistory with the native constructor and methods.

Chime logo
Chime
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Implement BrowserHistory with the native constructor and methods. Begin at homepage. visit(url) goes to a new page and clears every forward-history page. back(steps) and forward(steps) move as far as available, at most steps,and return the resulting current URL.

class BrowserHistory {
public:
    BrowserHistory(string homepage);
    void visit(string url);
    string back(int steps);
    string forward(int steps);
};

Examples

Example 1:

Calls: BrowserHistory("home"); visit("a"); visit("b"); back(1); visit("c"); forward(2); back(9)
Returned navigation values: ["a","c","home"]
Explanation: Visiting c from a discards b; forward stays at c,and the large back request stops at home.

Example 2:

Calls: BrowserHistory("start"); back(1); visit("next"); back(1); forward(1)
Returned navigation values: ["start","start","next"]
Explanation: Back cannot leave the initial page, while the later back/forward pair traverses the one stored visit.

Constraints

  • homepage and each url have length 1 to 20 and consist only of lowercase English letters or periods.
  • 1 <= steps <= 100 .
  • At most 5000 calls in total are made to visit,back,and forward.

Contract considerations

Use the constructor, visit, back, and forward methods shown above; successive calls share the browsing history.

Boundary reminders

Use the constructor, visit, back, and forward methods shown above; successive calls share the browsing history.