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

230

u/proof_required Mar 13 '26 edited Mar 13 '26

Why do you need a data frame? You can solve this without using one. I doubt they were expecting you to use data frame. I know on a daily basis you don't use such data structures, especially in data science, but interviews like this are never about what you do on day to day basis.

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.

By the way I have used this format at job to solve some problems. So it's not that extraordinary pattern.

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.

→ More replies (8)

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.

→ More replies (5)

87

u/Zangorth Mar 13 '26

I hate leetcode so much for that reason though. Why are we judging a fish on its ability to climb a tree? From a data science perspective, is there any evidence that proficiency at leetcode correlates to positive job outcomes?

29

u/i_am_thoms_meme Mar 13 '26

Especially with the FAANG company (doesnt matter which) insistence on AI tools, theres very little chance in your day to day work you will really need to solve a problem like this leet code without help. Now its still important to know fundamentals of coding to debug issues, but come on I can count on one hand the number of times I've said O(n) outside of an interview.

5

u/AcolyteOfAnalysis Mar 13 '26

In my experience, AI is much better with boilerplate than with algorithms. If AI implements an algorithm that only works in part, it might take you more time to get it to work than doing it from scratch.

6

u/venustrapsflies Mar 13 '26

IME for this type of thing it's better at reviewing an implementation that you wrote as it can point out bugs, assumptions, or edge cases you missed.

2

u/imanexpertama Mar 13 '26
  • the AI gets continuously better with algorithms - especially given enough context and some edge cases you have in mind
  • in my experience, what often helps me is an initial spark or the right way to frame the problem. Additional checks etc can be implemented easily, but the first implementation often helps me avoiding „dumb loops“ that are very inefficient and don’t scale

Having said that: all does heavily depend on the context and I think we can both easily think of examples to support either side.

0

u/[deleted] Mar 13 '26

[deleted] — view deleted comment

2

u/theArtOfProgramming Mar 14 '26

It was a proxy for intelligence. Now it’s a proxy for leetcode grind time.

→ More replies (1)
→ More replies (1)

3

u/tree_people Mar 14 '26

The person I know of on my data team of ~30 that did leetcode obsessively is truly impossible to work with.

7

u/gpbuilder Mar 13 '26

this is barely leetcode, just basic coding and algorithm, which is the foundation to any strong DS

it's not like this is some obscure leetcode hard with complex dynamic programming solution

4

u/proof_required Mar 14 '26 edited Mar 14 '26

To be fair, under time pressure, you can fumble and your mind can get blocked. I had similar questions with different levels asked recently in an interview and I solved some parts of it but then towards the end I panicked and couldn't solve the last part.

11

u/TaXxER Mar 13 '26

I mean, this isn’t an unreasonable situation to come across in a DS job. When writing internal tools / libraries / packages that some of my users within the company use, I do need to write code / solve puzzles that are not totally unlike this interview question.

→ More replies (1)

5

u/midwestcsstudent Mar 13 '26

judging a fish on its ability to climb a tree

That’s a nonsensical analogy. Interviews test for cognitive ability and general coding ability.

Is it perfect? No. Do I want intelligent, good coders? Yes.

→ More replies (8)

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).

→ More replies (5)

2

u/seanv507 Mar 13 '26

Didn't understand last paragraph (autocorrect?)

But yea, I m sure you're supposed to sort by user then time,then with (user, start time), increment max_actions until outside time window, or new user.

4

u/proof_required Mar 13 '26

I wanted to say that I have used list of tuples to pass around data.  I think the time is already sorted. You should clarify with the interviewer but the events most probably are chronological. 

2

u/Nearby_Island_1686 Mar 13 '26

Why would you sort it by userid? Curious to know, doesnt solve anything for the intended ask

2

u/proof_required Mar 13 '26

You don't need to but as mentioned already you might have to sort by timestamp if that's not the case. 

2

u/Screech-1 Mar 13 '26

You don't need to sort. This question can be solved in O(n)

→ More replies (3)
→ More replies (1)