Design structure for first unique login user
OracleSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to design an efficient online data structure that maintains a stream of login events and supports constant-time updates and queries for the earliest user with exactly one login.
You are given an online stream of user login events. Each event is a user ID (e.g., an integer or string) indicating that this user has just logged in.
You need to design a data structure that supports two operations:
-
login(userId)Record thatuserIdhas logged in one more time. -
firstUser()Return the ID of the user who
- has logged in exactly once so far, and
- among all such users, is the earliest by login time (i.e., the one whose first login happened earliest in the stream),
or return a special value (e.g.,
nullor-1) if no such user exists.
Requirements:
- Both
loginandfirstUsermust run in O(1) worst-case time per operation. - You can assume the number of distinct users fits in memory.
- You may choose any reasonable representation for
userId(e.g., integer or string), but specify your assumptions.
Example:
Suppose the login stream is:
login(A)// A has logged in oncelogin(B)// B has logged in once; A is still first uniquelogin(A)// A now has logged in twicelogin(C)// C has logged in once
Then the sequence of firstUser() calls should return:
- After
login(A):firstUser()→A - After
login(B):firstUser()→A(A and B are unique, A is earlier) - After
login(A):firstUser()→B(A is no longer unique) - After
login(C):firstUser()→B(B is still the earliest unique user)
Design the data structure and its operations to meet the time complexity guarantees. You do not need to handle persistence or multi-threading; focus on the core algorithm and data organization.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.