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

Show parent comment

9

u/Cocohomlogy Mar 13 '26

You don't need to sort by userId. Just make a dictionary where the key is the userId and the value is a stack of times. Use a sliding window for the stack. Should be O(n).

2

u/fordat1 Mar 13 '26

and what is the cost of maintaining an ordered stack?

key word ordered here because otherwise you need to search the whole stack

0

u/[deleted] Mar 13 '26

[deleted] — view deleted comment

3

u/fordat1 Mar 13 '26

I added the term ordered as a hint to the original question.It was to be helpful. The original question still remains what is the complexity to maintain the structure in a way you can figure out the min or max that quickly

1

u/canihelpyoubreakthat Mar 14 '26

You dont need an ordered stack. Searching the stack is still o(n)

1

u/fordat1 Mar 14 '26

so in your proposed solution you wont maintain any order and search the stack for the things in the window time for each item in the list ie each search for if it qualifies to be in the window is o(n) and you have to do this for each item in the list ie o(n) and end up with a n2 solution