1

I am trying to solve this Leetcode question: https://leetcode.com/problems/design-browser-history/

Below is my code:

class BrowserHistory:

    def __init__(self, homepage: str):
        self.history = []
        self.history.append(homepage)  
        self.currPos = 0

    def visit(self, url: str) -> None:
        self.history.append(url)
        self.currPos = len(self.history) - 1

    def back(self, steps: int) -> str:
        pos = self.currPos - steps
        if pos <= 0:
            self.currPos = 0
            return self.history[0]
        else:
            self.currPos = pos
            return self.history[pos]

    def forward(self, steps: int) -> str:
        pos2 = self.currPos + steps
        if pos2 >= len(self.history) - 1:
            self.currPos = len(self.history) - 1
            return self.history[-1]
        else:
            self.currPos = pos2
            return self.history[pos2]

When I run this, the sample usecase is failing:

INPUT:
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]

[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]

OUTPUT:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","facebook.com","leetcode.com"]

EXPECTED OUTPUT:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]

Not sure why back of 2 steps should give google.com instead of facebook.com.

Where am I going wrong? Any help would be appreciated.

Thank you.

3
  • 1
    Remember that the history after your current point is replaced when you "visit". It is not appended. Youtube is removed from the stack when you go to linkedin. Commented Feb 9, 2021 at 18:57
  • I couldn't figure that part out. let me try it with that :) Commented Feb 9, 2021 at 19:04
  • I spelled it out with diagrams and everything Commented Feb 9, 2021 at 19:07

1 Answer 1

2

When you visit a site, you expect that going back will take you to the site you came from, not the site at the end of your stack.

Let's say that you have the following history:

google.com -> facebook.com -> youtube.com
                                  ^
                             You are here

You then decide to go back to facebook.com:

google.com -> facebook.com -> youtube.com
                  ^
             You are here

When you go forward, you expect to be back to youtube.com. So far so good.

But what if you want to go and visit linkedin.com from here? Currently your code is appending to the stack:

google.com -> facebook.com -> youtube.com -> linkedin.com
                                                  ^
                                             You are here

That structure says that going back one will take you to youtube.com, even though that's incorrect: you came from facebook.com.

So the solution is not just to append: you have to truncate first:

google.com -> facebook.com -> linkedin.com
                                   ^
                              You are here

Specifically, rather than updating currPos from the length, update the length to match currPos:

def visit(self, url: str) -> None:
    self.currPos += 1
    del self.history[currPos:]
    self.history.append(url)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.