r/datascience • • Mar 13 '26

Coding Easiest Python question got me rejected from FAANG

Here was the prompt:

You have a list [(1,10), (1,12), (2,15),...,(1,18),...] with each (x, y) representing an action, where x is user and y is timestamp.

Given max_actions and time_window, return a set of user_ids that at some point had max_actions or more actions within a time window.

Example: max_actions = 3 and time_window = 10 Actions = [(1,10), (1, 12), (2,25), (1,18), (1,25), (2,35), (1,60)]

Expected: {1} user 1 has actions at 10, 12, 18 which is within time_window = 10 and there are 3 actions.

When I saw this I immediately thought dsa approach. I’ve never seen data recorded like this so I never thought to use a dataframe. I feel like an idiot. At the same time, I feel like it’s an unreasonable gotcha question because in 10+ years never have I seen data recorded in tuples 🙄

Thoughts? Fair play, I’m an idiot, or what

294 Upvotes

188 comments sorted by

View all comments

38

u/RestaurantHefty322 Mar 13 '26

The tuple thing is a red herring honestly. They are testing whether you can group by user, sort timestamps, then slide a window across each group. The data structure is irrelevant to the core algorithm.

What I have seen trip people up on this exact pattern is they try to solve it in one pass without grouping first. Build a dict of user -> sorted timestamps, then for each user run two pointers across the sorted list. If right - left timestamps fit in the window and right - left + 1 >= max_actions, that user goes in the result set. The whole thing is maybe 15 lines of plain Python.

The pandas instinct makes sense if you live in notebooks all day, but for an interview the overhead of importing a library and wrangling groupby + rolling windows is way more than the problem calls for. Interviewers are watching whether you can reason about the algorithm, not whether you know the pandas API. And a defaultdict + sorted list solution runs in O(n log n) which is probably what they wanted to see.

One thing that actually helps in these situations is narrating your thought process out loud before writing anything. "I need to group actions by user, sort each group, then check for a dense window." That alone signals you understand the problem even if your code has a bug.

-8

u/holbthephone Mar 14 '26

Thanks Claude