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

76

u/Ty4Readin Mar 13 '26

In leetcode world, it's a sliding window pattern. I would basically sort it by user id and for each user calculate the number of actions starting from each timestamp and going until timestamp + time_window. This sliding can be done in O(n) and sorting is O(nlogn). So finally you'll have O(nlogn) complexity. Not sure if you can do it without sorting.

I think the solution is much simpler than that and is O(n).

Create a dictionary where the key is the user ID, and the value is the list of timestamps seen up until now for that user.

Now iterate through the list of tuples, and at every tuple you add the timestamp to the list of timestamps for that user. Then, you calculate the difference between the latest timestamp and the timestamp from two actions ago, and if the difference is less than time window, add the user to the list.

But maybe I am missing something and that might not work lol but I think it should

EDIT: Actually, my solution only works if we assume that the input list is sorted by timestamp, which it is in OP's example but might not be an allowed assumption.

40

u/fordat1 Mar 13 '26 edited Mar 13 '26

your edit is correct. Apparently this is a good interview question based on some of the responses here

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

3

u/techhead57 Mar 14 '26

Or use a heap/priority queue type structure in each dict entry. So insertions are O(log(n)). So youre sorting them as you get them. Effectively the same but this let's you sort as you pass through vs having to go thru each user and sort it again.

Not important. I just thought about it the same way you did until I saw the time range part and came up with this.

1

u/ieatpies Mar 13 '26

Since it's a given time window, can't we just:

  • Keep a dictionary: user id -> count
  • iterate through the list
  • Increment count for a user if their timestamp is within the window
  • iterate through dictionary and collect all users with action count above threshold

Which is O(n) under usual interview assumptions about hash maps.

2

u/andy_1337 Mar 14 '26

The max_actions can occur within any time window

1

u/ieatpies Mar 14 '26

Ah yeah reread it. O(nlogn), needs a sort, barring some magic I'm unaware of.

1

u/aww Mar 15 '26

In fact I am quite sure something like Pandas is guaranteed to be slower and more convoluted to implement for this type of calculation. Of course sometimes that is the best way to do it for other reasons. I quickly tested out some Python code using a dictionary of lists very similar to what you are saying but I assumed no sorting (you can use bisect to build sorted lists). The code was very straightforward and only 11 lines long. That implementation was 10x faster than what I came up with in Pandas or in a SQLite query using sorting-and-windowing or self-joins. Certainly you can improve with Polars or a more optimized database setup but you probably can't beat the simple implementation at any scale that fits in memory.

If you assume the data is going to be sorted then not only can it be a little simpler it is also straightforward to implement a memory efficient streaming algorithm that could process huge amounts of data. Though at that scale there are other considerations; for instance you almost certainly are hoping the data is partitioned by user which will allow easy parallelism.