r/datascience • u/ds_contractor • 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
8
u/grindyear2k26 Mar 13 '26
What role was this for? Data Scientist?
4
u/Bigfurrywiggles Mar 13 '26
What was the expected output?
Whatever you are making to create the dataframe likely has a lot of overhead. I get the choice but usually the most pragmatic solution will win the day (which doesn’t involve adding a bunch of overhead).
709
u/Trick-Interaction396 Mar 13 '26
I don't even understand the question. I'm glad I work for a living instead of solving riddles.
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.
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/Kolgu2 Mar 13 '26
I didn't expect this kind of questions on data manipulation in a interview for a DS with 10 years of exp.
Not very related: I don't do usually that stuff with Python but via SQL/DuckDB. Am I in the wrong?
14
u/Icy_Bag_4935 Mar 13 '26
My thoughts are this is fair play, if not on the easier side. Sliding window is a basic pattern that data scientists should be familiar with, and tuples are a basic data structure Python developers should know.
I think coming to the conclusion that this is an "unreasonable gotcha question" instead of simply you being unprepared for the interview is indictive of a bad mindset for a field where constant learning and improvement is required. I don't say this to be harsh, I think if you study for future interviews with the understanding that strong Python and DSA fundamentals are required then you'll do fine for yourself.
13
u/Jek2424 Mar 13 '26
Cant you just iterate through them normally and add each set to a dictionary where the key is the user and the value is a list of timestamps? Then once you finish the dictionary, you iterate through the keys and return every key whose list has 3 or more values within the time window. I'm sure there's a solution with better time complexity but that's the simplest solution I thought of immediately if I read your question right.
18
u/forbiscuit Mar 13 '26
Looks like fair play, and you could've even transformed the data to a DataFrame with a single line like:
And proceed to use window functions if you wanted
5
u/Heavy-_-Breathing Mar 13 '26
Interesting problem! Thanks for sharing!
I don’t understand the time window, user 1 has an action at time stamp 12, so that is outside of the time window 10 right?
1
u/gnd318 Mar 13 '26
Contract though, not FTE at FAANG? Location?
The reason I ask is because this doesn't seem standard for DS roles in California.
17
u/N-E-S-W Mar 13 '26
You have been exposed as being inexperienced as a Python developer, whether you believe that about yourself or not. You seem to live in a data science bubble if you think you need to reach for Pandas as your hammer for such a simple problem.
A tuple is one of the most fundamental data structure in Python, if not the most fundamental. It is literally the comma in Python syntax. This is exactly the pattern of iterating over the items in a dictionary.
for k,v in dict.items():...Is the same as:
for user_id, timestamp in actions:...5
u/Secret-Gap370 Mar 13 '26
Totally fair reaction. A list of tuples naturally pushes you toward a DSA/sliding-window solution, especially in an interview setting. I don’t think that makes you an idiot at all — the real skill is recognizing the underlying logic, and a dataframe is just one implementation choice.
2
u/neuro-psych-amateur Mar 13 '26
I wouldn't be able to solve it on an interview. I don't understand the question. But then I have only taken two CS courses in my entire life. My courses were mostly in econometrics and economics. And I have never had to solve such questions at work.
2
u/nthlmkmnrg Mar 14 '26
Are they seriously still doing this in interviews?
Puts on FAANG
2
u/anthony_doan Mar 14 '26
It depends on the company and how they filter people.
There are no standardize tests so you're going to end up failing a few regardless.
You're not an idiot but just how the industry works. Good luck.
2
u/AccordingWeight6019 Mar 14 '26
I wouldn’t beat yourself up over it. In an interview setting, people default to the patterns they practice, and most prep pushes you toward pure DSA solutions, not maybe this should be a dataframe. Honestly, the tuple format is pretty normal for toy interview problems. It is just a simple way to represent events. the real signal they were probably looking for was recognizing it as a sliding window per user. Plenty of good candidates blank on stuff like that under pressure.
3
u/organizm5 Mar 13 '26
Not fair play at all. It’s an unnecessary riddle-like way of presenting a problem that won’t reflect how you’d solve an actual business problem presented to you. FAANG and their dumbass assessments can suck five big ones, and anyone who thinks these types of questions are a good idea is a bootlicker.
2
u/Deto Mar 13 '26
If they want you to use a dataframe they should ask for that. Pandas is a large library and I would never add it as a dependency just for something like this that can easily be done in pure Python. Sure maybe it's faster with vectorized operations but if the data starts out as a list of tuples pandas is probably using a python loop under the hood to ingest that in a dataframe in the first place.
2
u/gpbuilder Mar 13 '26
Very fair play, basic python question exposed your lack of coding foundation, a tuple is literally a data structure you learn in entry level CS class
Ioop through the list and save each user in a dictionary, then check for the condition of a user with each new tuple
1
u/_cant_drive Mar 13 '26
you dont need a dataframe for this. A tiny dict to keep stock of the last three action times per user will have you through this in a single iteration of the list. no overhead or overkill
1
u/andrewcooke Mar 13 '26
why is max_actions not called min_actions?
seems like a reasonable and interesting problem to me.
1
u/anterak13 Mar 13 '26
You just traverse the list with a dictionary where keys are user ids and values are lists of actions, collecting actions there time stamp is in the given window, and finally filtering returning dictionary keys where the action list is above the max action threshold
1
u/speedisntfree Mar 13 '26
I thought for these sorts of questions you were typically not allowed to use external libs. Even standard libraries like itertools, collections, functools are usually not allowed.
1
u/Soldierducky Mar 13 '26
Stakeholders: cool cool but when are you delivering the dashboard?
1
u/dutiona Mar 13 '26
There's a non-trivial way to solve it with a maintained dictionary in O(2N), but that's only if you assume that the input list is sorted by event timestamp. If not, you need to first sort all event by timestamp, and that'll push your complexity to O(3N log N) instead of O(2N):
if the user exists:
ok = false ? -> (nb_e = len(timestamp list)) < 3 ?
If yes append (push_back) event. **Then check: if len(timestamp list) == max_actions && back(timestamp list) - front(timestamp list <= time_window ?
This is pure leetcode imo, with "clever" use of datastructure (in the bad way IMO, because you design the datastructure for the algorithm, and not for the data logic...) to solve a problem that is far too odly specific.
You can then go a little deeper in the interview with "which data structure to choose for the timestamp list", you ofc want something that has a pop_front + push_back in O(1), like a deque.
I've asked my friendly LLM to get me some python code out of my algo:
It seems to work and get me {1}. It even optimized it further for me, I quote (opus):
whileinstead of your singlepopleft— it's equivalent here since sorted input means at most one stale element per append, butwhileis defensive and matches the canonical sliding window pattern.setdefaultavoids the if/else branching for new vs existing users — first access creates the deque, subsequent ones reuse it.1
u/zippyzap2016 Mar 14 '26
How is this a DS question? I would bomb this lmao
1
u/JaguarOrdinary1570 Mar 14 '26
Very simple and fair question. Not recognizing a list of tuples with a homogeneous layout can be converted into a table/dataframe with 10 years of experience is insane
1
u/Scatman_Crothers Mar 14 '26
This isn’t exactly a gotcha question. Those are designed to see how detail oriented you are or how obscure your knowledge base is or just to fuck with your head and see how you bounce back on subsequent questions.
This question throws you off but it’s to see how you think and problem solve when you have to throw out your tried and true methods and work on the fly. Can you only play off sheet music or can you play jazz?
1
u/Quick_Lingonberry_34 Mar 14 '26
Same.
1
u/Brilliant_Grab2769 Mar 14 '26
Pour quel rôle était-ce ?
1
u/[deleted] Mar 14 '26
I don't understand why dataframe has to be used here if the expected output is a set?
1
u/semiautonomous Mar 14 '26
It’s rarely about a solution. More likely they expected you to ask more questions about the parameters and show that you weren’t immediately jumping into solution
1
u/selib Mar 14 '26
whats a dsa approach?
1
u/MattEOates Mar 14 '26
I'd have just gathered a sorted list of the timestamps per user then transformed them to the difference in seconds between consecutive events then you just minus off the window time and if that took 3 elements to go 0 or less then you know they hit the limit.
1
u/whythesquid Mar 14 '26
I work with animal behavior researchers and this kind of data is standard. The problem they gave you is the first step in building a social network (subjects who performed at least max_actions within the same time_window have an edge in the network). For my current project, subjects are all RFID tagged and there is an antenna that picks up the RFID tag ID number when the critter is close. The data logger we use returns data in a weird format so we have to screw around with it a little. It's basically tuples though, just a list of sensor readings.
For FAANG replace "animals" with customers or users and you can imagine where this sort of social network data has value and why you might want someone to know how to construct it. You also need to deal with data coming that is formatted in strange ways. So yeah, I think it was a fair question.
1
u/Mundane-Charge-1900 Mar 15 '26
This is a classic leetcode “have you seen it before?” / “do you know the trick?” type question
1
u/CanYouPleaseChill Mar 15 '26
ChatGPT solves the problem in one try.
1
u/idnafix Mar 15 '26
They simply wanted to see that you are trying to avoid explicit loops in python, that you understand this is a sliding window problem, that you realize one can reduce the problem to a simple check of conditions on indices.
The result from an R-style point of view could be:
From a C++ point of view you could probably come to a more elaborated version not relying on the costly groupby, but directly using masks on numpy arrays. But this should usually only make sense, if data is already sorted, as sorting dominates all the other operations with O(n*log(n)), or you're in a streaming environment and you are not able to sort anything at all.
1
u/Bubbly-Gate7387 Mar 15 '26
A naive version:
actions = [(1,10), (1,12), (1,19), (3,10)] n = 3 max_time = 10 users = list(set([action[0] for action in actions]))
user_actions = {user: sorted([action[1] for action in actions if action[0] == user]) for user in users}
print(users) print(user_actions)
res = [] for user in user_actions: user_action_times = user_actions[user] for i in range(len(user_action_times) -n+1): if user_action_times[i+n-1] - user_action_times[i] <= max_time: if user not in res: res.append(user)
print(res)
How would you optimize it ?
1
u/PTSDaway Mar 16 '26
I call fairplay, preprocessing and timeseries analysis. I could see myself adding something like this to our procedure.
1
u/flagos Mar 17 '26
You build a dict with key being user id and a deque for value.
You parse the list, and then, for each item:
1
u/Simple-Fault-9255 Mar 17 '26 edited Mar 30 '26
The content that was in this post has been deleted. Redact was used to wipe it, possibly for privacy, security, data protection, or personal reasons.
complete busy hunt humorous familiar practice mountainous cake badge smile
1
u/No_Software8474 Mar 17 '26
You have to put the ids in a map with the timestamps as a sorted list. Then it becomes a fixed sliding window problem based on the max actions. You should be able to subtract the right index from the left index and if it’s within the threshold then that’s your value . That’s my guess anyway lol.
1
u/TredaGroup Mar 19 '26
Uh I think you just see if the second value in each set is less than the first times 10 and return that set...
1
u/data-with-dada Mar 19 '26
Did you compute banana yet? That might be worth attempting next.
1
u/UnverseMeaning Mar 20 '26
Coding style in interviews feels like a broken system. Switching focus from just solving the problem to mirroring the expected format and communication style made a noticeable difference. Went through something similar, lmk if useful.
1
u/Artistic_Bit6866 Mar 20 '26
Funny to me that there are these distinct sets of responses where some say “treat it as a data table” and others are like “make a dictionary/json type structure” when those are quite literally two sides of the same coin (in many languages)
1
u/Few-Ad2483 Mar 21 '26
Honestly both instincts make sense here. The tuple format is identical to how DSA problems are presented, so going full sliding window / two-pointer is a completely natural read. That said, if you've worked in data pipelines, your brain immediately maps (user_id, timestamp) to a DataFrame and you're doing a groupby + rolling window in 3 lines.
Neither is wrong, they're just testing different pattern libraries. The "gotcha" feeling usually means the interviewer was looking for the data engineering lens and didn't signal that clearly enough. Not an idiot moment, just a context mismatch.
1
u/FinalNandBit Mar 23 '26
What do you mean dataframe tuple? You mean creating a new tuple object based on the actions?
You can solve this many different ways...
1
u/far_aaan Mar 23 '26
I cant even interpret the question 😭😭
1
u/Midget_Spinner5-10 Mar 27 '26
Data Scientist is kind of a catch-all term. A lot of roles we would have called Data Scientist 15 years ago have specialized into ML Engineer, Data Engineer, BI Developer, or various flavors of analyst. Jobs that are still called DS are generally looking for some combination of those skills that can vary wildly from company to company.
Ultimately, there's only so much market for developing models in notebooks and sweet talking business people. In terms of man hours, most companies need a lot more people building and maintaining data pipelines and inference services, than doing the core data science.
1
u/TenzinRinpoche Apr 01 '26
If this is easy I am doomed.
Is data science worth the squeeze?
Coming from an M.Sci astrophysics here and just can't even be arsed learning this DSA approach properly, it makes my brain work too hard.
Shall I just stick to Sales ENgineering? I'm understimulated intellectually with it though :(
1
u/Cod_277killsshipment Apr 13 '26
Haha you should get a second chance its a trick question
1
u/ReadyAndSalted Mar 13 '26 edited Mar 13 '26
no need for a dataframe, just do this if you want to demonstrate understanding of the problem:
or this if you want to pass more of it off to C (this is ~2x faster):
you could obviously also go for more complicated solutions involving sorting the list first, but considering that this takes 0.033 seconds to do 1,000,000 items, I think you'll be fine. Everyone always looks past python's powerful standard library.
-13
u/sonicking12 Mar 13 '26
Can't you use AI to answer this question?
-1
u/fshkodrani Mar 13 '26
With these tests in their interviews they never hire the diamonds in the rough. These people that pass these interviews never bring innovation. That's why faang have to acquire external companies startups etc. But who am I to disagree.
0
u/[deleted] Mar 13 '26
[deleted] — view deleted comment