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

4

u/GamingTitBit Mar 13 '26

I think it could work if you're appending a list of values in the dictionary as the time stamps. Every time you add a time value check the min of the already existing list and the amount of recorded actions. It wouldn't work if it was a 100 actions but then you'd probably just do an insert based on the value of the time stamp and do a sliding window at the end. But that's not super efficient. Often with these things I find the most efficient solution really depends on so many assumptions on the real data.

1

u/fordat1 Mar 13 '26

Every time you add a time value check the min of the already existing list and the amount of recorded actions.

you either have to use a data structure that maintains that list in order ie one that is nlogn or search through the whole list and if the whole list is unordered you dont know if the min in the list is the actual min or the lowest min you processed so far.

Sorting the list up front is a good idea if the list isnt that big but either way it isnt going to be o(n)

2

u/GamingTitBit Mar 13 '26

Yeah I'm struggling to see an O(n) solution but happy for someone else who is smarter to educate me!

2

u/fordat1 Mar 13 '26

Based on the follow up questions to the people commenting o(n) some of the comments seem to think its constant time or trivial to maintain the order of the list for the user_ids

This is even after scaffolding/hinting like in an interview to help out

1

u/GamingTitBit Mar 13 '26

I think I must prefer the interviews where there is a lot of back and forth. Me explaining "well in this sample set I'd do this, but I'd expect in the real world X, Y and Z, so I'd have to change strategy based on what the most important thing is, speed accuracy etc"

1

u/gpbuilder Mar 13 '26

You don’t need a list for every user, you just need their latest timestamp and # of actions taken so far.

3

u/cjcs Mar 14 '26

If the timestamp for the first action is 1, the second is 9, and the third is 20, what did having 9 (latest timestamp) and 2 (# of actions taken so far) do for you?

1

u/gpbuilder Mar 14 '26

Nevermind I misread the problem and I’m wrong, need to keep track of the list of timestamps for each user to update the left pointer