← Questions
Oldest one-time visitor in a login stream
0
I was asked this
No reports yet. Sign in to say where you were asked it.
An online store wants to know which customers have visited only once. Every login is reported to your service as a username, in the order the logins happen.
Implement a class OneTimeVisitorTracker with:
-
login(user: str) -> None: record one login byuser. -
oldest_one_time_visitor() -> str | None: among all users who have logged in exactly once so far, return the one whose login came first. ReturnNoneif there is no such user.
A user who logs in a second time stops being a one-time visitor for good, no matter how many more times they log in.
Constraints
- Up to 10^6 calls in total; usernames are non-empty strings and case-sensitive.
- Aim for O(1) time per call.
Example 1
login("ana"); login("ben"); login("cy")
oldest_one_time_visitor() -> "ana"
login("ana")
oldest_one_time_visitor() -> "ben"
Example 2
login("ana"); login("ana"); login("ana")
oldest_one_time_visitor() -> None
login("dee")
oldest_one_time_visitor() -> "dee"
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding

