Dsa
Stack
Medium
URL Tracker with Stack
Create a URL Tracker that allows for navigation through websites using a simple command interface. You can go to a new URL with 'visit URL', and you can go back to the last URL with 'back'. If the back command is invoked and there are no previous URLs, return 'No URL to go back to'. Implement the tracker so that the 'back' command only returns the last URL, it should not keep a stack of visited URLs.
Input Format:
- Multiple commands, each being either 'visit URL' or 'back'. The first command will always be a 'visit'.
Output Format:
- Return the most recent URL or a message if no URL is available.
Example:
Input:
visit example.com
visit google.com
back
visit stackoverflow.com
back
back
Output:
google.com
example.com
Key concepts
stackurlnavigation
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now