← Questions
Country with the most events
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Notion's technical phone screen, last asked Mar 2026. It tests events and aggregation.
You have a product event log and a user table that records each user's country. Join the two, count events per country and return the country with the most events. Write it in Python, and be ready to write the same thing in SQL.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
events: rows of(user_id, event_type, timestamp). -
users: rows of(user_id, country). -
top_country(events, users) -> str | None— the country with the most events, ties broken alphabetically. Events from users missing fromusersare skipped. ReturnsNoneif nothing is left.
Example
users: 1 US, 2 JP, 3 JP, 4 US
events (user_id): 1, 2, 3, 3, 4, 9
US: users 1 and 4 -> 2 events
JP: users 2 and 3 -> 3 events
user 9 has no row in users -> skipped
top_country(events, users) -> "JP"
Constraints
- Up to 10^7 events and 10^6 users. A user has at most one row in
users.
Clarifying questions
- Should events from unknown users be skipped, or counted under an "unknown" country?
- Is the country the user's current one, or where they were when the event happened?
- Count events, or distinct active users per country?
- Return one country on a tie, or all of them?
What a strong answer covers
- A hash map from user id to country, then one pass over events: O(e + u) time.
- The SQL:
JOIN users USING (user_id),GROUP BY country, ordered by count then name; and what aLEFT JOINwould change. - Handling duplicate user rows and null countries.
- Normalizing the result, such as events per active user, if the product question is about engagement rather than size.
Follow-ups
- Return the top country for each event type.
- The users table is too large for memory: what does a database (or a sort-merge join) do instead of a hash map?
- The product team wants the country with the most events per user. Write it, and explain why the answer differs.
Where it's asked
- Notion · Technical phone screen · Last asked Mar 202660 min · Live coding in CoderPad
What it tests
events · aggregation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
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
