5 ms·
I interview a lot of people for frontend positions. I ask one two-sum question to see if they have some knowledge about data structures and simple algorithms.
by wry_discontent 6y ago
I interview a lot of people for frontend positions. I ask one two-sum question to see if they have some knowledge about data structures and simple algorithms. Then I ask them to build something trivial in React, which is what we use. If you can do those things, you're probably fine. The number of people who can't is truly shocking.
- fractionalhare 6y agoI would argue there are many well-qualified frontend engineers who won't correctly solve the two sum problem on their first time encountering it.
- user5994461 6y agoThis one? https://leetcode.com/problems/two-sum/ https://leetcode.com/problems/two-sum/ >>> Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Seems trivial enough to me... except if the interviewer forgets the hard constraint that there is exactly one solution, then that's a shitstorm of edge cases. I'd criticize the problem for asking for the positions of the numbers rather than the numbers. That's just super annoying for no reason.
- fractionalhare 6y agoDoes the problem actually have that constraint? If so then yeah it's significantly easier. I thought the reason you'd have to use a clever algorithm is because a brute-force solution to find all index pairs would require n^2 operations, where n is the size of the input array, because you can't stop after you find a single pair.
- user5994461 6y agoYes it does, you can open the leetcode and see for yourself. That also explains the "easy" difficulty rating. I guess you've been giving candidates a more hardcode version of the problem, not having that constraint? edit: nevermind, it's the other poster who was giving that problem, not you. No idea how they formulate it in their interviews.
- fractionalhare 6y agoI don't ask candidates these kinds of questions in my interviews. For a coding interview, I'd normally ask them to implement something like backend rate limiting logic for an authentication service, and continually ratchet up the complexity on each successful implementation until the end of the interview. I was asked the two sum problem in an interview once, but I forget if it had this constraint or not. This seems much more reasonable.
- zebnyc 6y agoForgive me for my ignorance, but are you looking for anything more complex than a leaky bucket algorithm + gossip protocol solution?
- Viliam1234 6y agoTechnically, if all values in the array are the same value X, and the target is 2X, you can't do better than O(n^2), because just writing writing all valid solutions is O(n^2). If you can create a new array containing indices to the original array, sort the new array, and use two pointers starting at the beginning and the end of the sorted array to find a solution, it takes O(n×log(n)) time. It is not necessary to assume that the solution is unique, assuming it is okay to simply write one of the possible solutions. If all values are between 1 and k, and you can make a k-sized array, you can find a solution in O(n+k). No matter what you do, you can't get better than O(n). The important thing is that there is more than 99% chance you will never have to solve this problem in your job; and even in the case you would, there is 95% chance that the O(n×log(n)) solution would be acceptable regardless of whether it is the best possible or not.
- boomfus 6y agoIf we're being very charitable I guess the idea of using a HashMap for an optimal solution might slip your mind but even a freshman in CS can just loop twice over an array and solve it.
- fractionalhare 6y agoI might be misunderstanding the question; how many pairs can there be? You can't just loop over the array twice to find all qualifying pairs of indices, if there are multiple. You'd have to loop over the array n times, where n is the size of the array. Isn't that why the question is difficult? There doesn't seem to be much incentive to using a hash table, complexity wise, if you could just solve it in O(n) time and O(1) space by naively iterating over the array.
- renewiltord 6y agoHe meant "write two loops" when he said "loop twice". It seemed obvious to me that he meant that but I can see how if you're used to more unambiguous language it could be confusing. Yeah, you just do it in O(n²) worst case because the problem is O(n²) to find all pairs that match because there can be O(n²) pairs. However he meant one pair here too. Pretty easy to solve. Did it in under 30 s in Python on my broken cellphone. If there are multiple pairs, just replace return with yield.
- Keyframe 6y agoSame here. Absolutely shocking how many people fail at basics, yet they pose as mids or seniors even. I'm not even talking about CS, I'm taking about simple React-based assignments designed to show you know (at least) the basics and takes as little as time as possible to be fair from our side not taking too much of interviewee's time. Take what you want from it, but I've seen it mainly in web dev related positions.
- microtherion 6y agoYes, I see people fail at basics in interviews all the time. But I keep wondering how much of that is people being poorly qualified, and how much of it is interviews being poor indicators of true skill. I've seen people barely sneak through technical interviews, whose subsequent performance justified the concerns I had at the time. I've seen others hired against my personal recommendation who turned out to be brilliant at their jobs.
- Keyframe 6y agoOf course. That's the primary reason I'm personally against tasks on the interview itself or whiteboarding. That's usually a shitshow. We first do an interview where we talk a bit about our company and the interviewee, we get to know each other out a bit and try to find out if we're a good match for each other, based on the talk itself. If that's the case, we give a small (really small and basic, unless a senior position) assignment to take home and get back to us in five days. People have schedules, obligations, we understand. During the period we're always available for any questions, doubts, whatever you need. Assignment itself takes a couple of hours at most for someone that knows / has used the tech. At home, you can use google, stackoverflow, friends, books, I don't care. No one cares, as long as you show up with a consistently-looking code that works and that we can comment together on. You know, same as at work. Something to talk about for a bit, if at all. It's the basics. A damn good filter, it turns out.