3 ms·
Congratulations, you are now better than 2/3 of applicants I saw. I'd follow up by asking whether there's another way you could have done this (the obvious loo
by mkozlows 12y ago
Congratulations, you are now better than 2/3 of applicants I saw.
I'd follow up by asking whether there's another way you could have done this (the obvious loop and store method); if you didn't come up with it, I'd explain it to you, paying attention to how well you seemed to understand it. I'd go on to ask about what characteristics make one of those solutions better than the other, hoping you'll mention the performance characteristics of sort vs. simple iteration, and maybe also the maintainability/extensibility of the two methods.
And then I'd move on to my next question with cautious hope.
- Luyt 12y agoAnother way to do it, assuming that the list can be mutated: find the largest number in the array, store it somewhere and remove it from the list. Do this a second time. Then add those two numbers. It evades sorting the array, but you have to search it twice. a = [7, 9, 2, 8] highest = max(a) a.remove(highest) nexthighest = max(a) a.remove(nexthighest) print highest + nexthighest Optimisation: just scan the array once, and keep track of the two highest numbers. a = [7, 9, 2, 8] highest = None nexthighest = None for item in a: if not highest or item > highest: highest = item elif not nexthighest or item > nexthighest: nexthighest = item print highest + nexthighest
- mden 12y agoFyi, there's a mistake in your second solution - consider a strictly increasing list.
- Luyt 12y agoThanks for the tip! a = [2, 7, 8, 9] highest = nexthighest = None for item in a: if not highest: highest = item elif item > highest: nexthighest = highest highest = item elif not nexthighest: nexthighest = item elif item > nexthighest: nexthighest = item print highest + nexthighest
- FLUX-YOU 12y agoAnd my beef is with the huge variety of interview techniques (sorry if it felt like I was calling you out there, I wasn't trying to) and that candidates have to prepare for a whole range of them. One answer gets a spectrum of responses and there is rarely any good answers for everyone. (Maybe I'm just stating the obvious here)
- ryandrake 12y ago2/3 is generous. That implementation probably puts you in the top-90% of candidates that make it to a technical interview.
- madcaptenor 12y agoYou mean the top 10%?
- ryandrake 12y ago2/3 is generous. That implementation probably puts you in the top-90% of candidates that make it to a technical interview.