r/programming • • May 09 '15

"Real programmers can do these problems easily"; author posts invalid solution to #4

https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k Upvotes

1.3k comments sorted by

275

u/eddiemon May 09 '15

Problems 4 and 5 were pretty stupid tbh. I couldn't believe the original post got upvoted in the first place.

92

u/gnuvince May 09 '15

I didn't think so. #4 showed that there are a lot of edge cases that you must consider, and a candidate showing in an interview that they can think of those is highly valuable. #5 has many things going for it too: see if the candidate can recognize that brute force is a practical solution here, see how they handle constructing the expressions (linear strings or trees), etc.

I thought that problems 4 and 5 were very good questions if the goal is not necessarily to get a perfectly right solution, but to get a conversation going between the interviewer and the candidate. In actual fact, a member of another lab at my university recently had to answer question 4 during an interview with Google. He thought the question was really interesting and reportedly enjoyed the back and forth this created with his interviewer.

25

u/bat_country May 09 '15

Someone eventually showed that #4 succumbed easily to brute force instead of being clever...

def q4(list)
  puts list.permutation.map(&:join).max
end

I was actually really pleased when that answer showed up

3

u/drink_with_me_to_day May 09 '15

this is pretty,what language is it?

→ More replies (3)

1

u/SilasX May 10 '15

4/5 are great questions, but not as "basic competency questions", as the author was trying to use them. The author himself proved that you can get it wrong (in a limited time context) and still be a generally good programmer.

45

u/FlyingBishop May 09 '15

1-3 are great because they give pretty strong confidence that the interviewee hasn't actually spent much time coding.

4-5 are not so great because they're a little trickier and don't necessarily reflect on-the-job skills.

(Although, in an interview I'm not usually looking for the "right answer" I'm looking for something that looks like it could be the right answer, which is pretty easy to get to for 4-5. It's somewhat unfair to expect people to find all the edge cases for a potentially unfamiliar problem in an hour.)

25

u/donalmacc May 09 '15

4 perfectly reflects on the job skills. It's an arbitrary problem that could be anything, but has a whole pile of edge cases that the candidate must consider. I don't know about you, but in my day to day work I have to consider edge cases all the time.

→ More replies (8)
→ More replies (25)

39

u/ILikeBumblebees May 09 '15 edited May 09 '15

5 has many things going for it too: see if the candidate can recognize that brute force is a practical solution here

I actually started running through an imagined interview scenario in my mind, in which I explained to the interviewer that brute force seems to be the most effective solution to this, but because of the small total number of permutations, runtime performance could be trivially optimized by using a prepopulated lookup table, which would take up only 19,683 bytes of memory in the worst case, but since each sequence of operators can be represented as a string of 16 bits, it might be possible to treat the string of operators itself as a pointer, and therefore to put the lookup table in only 6,561 bytes, which itself is eight times as much memory as you really need, since you're only trying to store boolean values which represent whether a given sequence of operators causes the digits 1-9 to add up to 100, so you could lop off the last three bits of that 16-bit string and pack the boolean values for eight operator sequences into a single byte, using the three bits you chopped off as an index to which bit in that byte represents the value for the queried sequence, resulting in a lookup table that only takes up 821 bytes; then I accidentally spilled my coffee on the interviewer and didn't get the job.

31

u/Slime0 May 09 '15

Wouldn't the process of making a lookup table require computing all permutations of the operators, in which case it's better to just print the ones that work as you find them? What's the benefit of the lookup table?

38

u/jrmrjnck May 09 '15

Yeah, the point of the question was basically to calculate that table. I don't know what he's going on about.

→ More replies (8)
→ More replies (28)

22

u/argh523 May 09 '15

This, gentleman, is why "premature optimization is the root of all evil".

→ More replies (1)

12

u/wllmsaccnt May 09 '15

Then I accidentally spilled my coffee on the interviewer and didn't get the job.

I would absolutely still hire someone if they spilled coffee on me if they seemed like they knew what they were doing on the coding end.

→ More replies (3)

6

u/njharman May 09 '15

As your interviewer I'd ask why you wasted so much effort on optimization? Was thre a business case? Did you profile? What are the hotspots? Do you always preoptimize? What other things could you have accomplished if you hadn't spent so much effort on efficiency? What is more important developer efficiency or code efficiency?

→ More replies (18)

7

u/more_oil May 09 '15

It's all in how the question is framed. "Do this in one hour or you aren't a real programmer" is bad, "I'm interested in seeing your thought process and approach to problem solving" is good.

→ More replies (1)

2

u/NinlyOne May 09 '15

if the goal is not necessarily to get a perfectly right solution, but to get a conversation going

This right here. The biggest problem with the original blog post was the attitude that if you didn't nail it in under an hour, you should (to paraphrase) reconsider your career choice.

2

u/Dug_Fin May 09 '15

I thought that problems 4 and 5 were very good questions if the goal is not necessarily to get a perfectly right solution, but to get a conversation going between the interviewer and the candidate.

I agree. I don't think the problems themselves are bad tools for an interview. I think the only issue is the blogpost dude saying crap like:

Here is the deal: if you can't solve the following 5 problems in less than 1 hour... you need to stop calling yourself a "Software Engineer" (or Programmer, or Computer Science specialist, or even maybe "Developer".) Stop lying to yourself, and take some time to re-focus your priorities.

He set himself up as a perfect target for his own test, and was felled by his hubris. In the end, he really illustrated the fact that programming tests in hiring shouldn't be some pass/fail arrangement, but rather should be like you say, a conversation starter.

2

u/notfancy May 09 '15 edited May 09 '15

For me #4 is a surprisingly subtle and very pleasing problem. I think it worthy of one of Richard Bird's pearls: how to get from the obvious but inefficient specification:

prob4 :: Int
prob4 = read . maximum . map concat . permutations . map show

to a sort-based efficient solution. The surprising comparator s [<] t ("comes before") defined as s.t > t.s in lexicographical order requires recognizing that strings of digits s and t are in order whenever for strings a, b, c you have a.s.b.t.c > a.t.b.s.c which is implied by s.t > t.s. That is not a cheap insight to come by. The further property that s.t > t.s iff s.s.… > t.t.… is subtler still.

All in all I find it a potent brain-teaser.

→ More replies (6)

59

u/JustinsWorking May 09 '15

5 was kinda fun; not as an interview question but just as a little problem to code up.

8

u/just_plain_me May 09 '15

Nah they were both fun!

→ More replies (3)

17

u/manghoti May 09 '15

Stupid how? Stupid in the assertion that you could do them in an hour? Or just stupid questions?

Personally I found 4 to be interesting, and I already knew about 5, and also found that one interesting.

106

u/eddiemon May 09 '15

The original post was "Five programming problems every Software Engineer should be able to solve in less than 1 hour" as if it's some golden litmus test for software engineers, but 4 and 5 are really just cute puzzles (not even that cute tbh) that are highly unlikely to show up in real world. It's like recruiting a professional basketball player based on their ability to make trick shots.

The clickbait title was fucking retarded too.

61

u/Snoron May 09 '15

Yeah.. and the person who posted it clearly didn't solve it in an hour, therefore they are an idiot :D

25

u/danweber May 09 '15

4 depended on seeing the trick. Depending on seeing a trick during an interview is bad form.

5 was easy if using a language with an eval().

22

u/Slime0 May 09 '15

I don't think I agree that #4 depended on seeing any trick. I think it was a more difficult problem than the blogger thought it was, but it's definitely something you can think through, try a solution in your head, find a counterexample, and refine your solution. I wouldn't expect anyone to solve it in an hour necessarily, but I would expect them to think it through thoroughly enough to realize that it's difficult and be able to explain why. It's actually a pretty good problem for watching someone's problem solving process.

→ More replies (14)
→ More replies (1)
→ More replies (4)

4

u/magmapus May 09 '15

Me too. I actually spent a few minutes prototyping a couple different ways to do it. I think all of those would be valid interview questions, certainly better than FizzBuzz for evaluating logical thinking.

3

u/Kinglink May 09 '15

Five is a pretty simple problem, it's a little hard and very interesting.

Four is just ugh city. Too many gotchas in an hour.

6

u/Slime0 May 09 '15

I was surprised that the original thread had a lot of posts saying that #5 was difficult without mention of #4. They both seemed obviously solvable by brute force, but #4 gave the feeling that it could be solved more smartly than that, which made it a lot more interesting/difficult to me.

3

u/Kinglink May 09 '15

The problem of 5 has a very narrow scope as you have a set scope of numbers, the problem of number four could have anywhere from three numbers offered to three hundred. Just my read on it and why I find the brute force solution appropriate in five but a last resort in four.

He also seems to miss a trick with four. Each number needs to be graded on it's largeness compared to all others not the number itself, so 860 is smaller than 87 but both are smaller than 8. He talked about just adding 0 after the numbers and it's a good thought but wrong. He should have added the last number over and over to compare the values (8 becomes 888 for comparison 87 becomes 877) but that's just my quick thought on it ( which I admit I had after reading his mistake)

→ More replies (7)
→ More replies (1)
→ More replies (8)

532

u/Fidodo May 09 '15

"Real programmers" understand that even seemingly simple problems can have unexpected complexities and weird edge cases can appear anywhere.

12

u/TheFeshy May 09 '15

Even simple, decades-old algorithms can have these unexpected complexities.

7

u/pvg May 09 '15

You call that decades? I've got your decades right here!

http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html

"In Programming Pearls, Bentley says "While the first binary search was published in 1946, the first binary search that works correctly for all values of n did not appear until 1962." The truth is, very few correct versions have ever been published, at least in mainstream programming languages."

→ More replies (1)

3

u/[deleted] May 09 '15

The binary search algorithm from the Programming Pearls book was famous for that. It bisected by evaluating (lower + upper) / 2, which (as 32 bit machines approached their limits) has an unnecessary overflow issue. Using a signed integer type makes the problem hit even sooner. Though it takes a very large array of very small items to hit the limit anyway, and switching to 64 bit integers avoids the bug anyway - at least for now.

→ More replies (1)

2

u/[deleted] May 09 '15

I'm thankful Zmodem is dead as a protocol. It was trivial to get the reference implementation to drop core.

→ More replies (2)

11

u/TheKillingVoid May 09 '15

"For every complex problem there is an answer that is clear, simple, and wrong."

H. L. Mencken

2

u/coonskinmario May 09 '15

Maybe this would lead to a better interview question: instead of asking them to solve these, ask the interviewee to sort them by difficulty and discuss why.

2

u/whooshayay May 09 '15 edited May 09 '15

This is one of the reasons we have tests! :-)

In any case, this problem given above is trivially testable by randomly generating input and verifying with brute force. This is the first thing I would have done before building a better algorithm. (Generate random input + cunning test cases, convert input to string, build all combinations by string concatenation, convert to them to int, take max value)

So it's hilarious that OP's solution is broken, especially when OP is using it to demonstrate some kind of programming test. Sure, everyone screws up but not testing is just plain bad.

3

u/Fidodo May 09 '15

It perfectly shows what's wrong with the guy's thesis. To be a great programmer you don't need to be great at algorithms at all, you need to be good at making clean, maintainable, testable, and robust programs. You can do all that and even be shit at algorithms.

2

u/[deleted] May 09 '15

Real programmers have learning curves.

→ More replies (12)

575

u/[deleted] May 09 '15

If you just ask questions and grade solely on the correctness of their solution, you're simply interviewing wrong.

A good technical interview requires discussion, whether it's high level, low level, or both.

Everybody makes mistakes - if you don't know that, you shouldn't be responsible for hiring. Aside from their ability to code, it's also important to assess how a candidate approaches problems, how they communicate, and how they respond when they're told they're wrong.

154

u/fenduru May 09 '15

We've turned candidates down for being overly focused on "finishing the solution". I don't need to know the solution, I just want to see how you operate.

I actually think it would be neat to have the interviewer be given the problem to solve at the same time as the candidate. This way you'd be testing how well they could work with the team, problem solving, and generally mistakes are fine if when called out you have a "oh, duh" moment rather than being clueless as to why your mistake was wrong

82

u/bonafidebob May 09 '15

I like this idea, at least for junior interviewers. Give the interviewer a sealed envelope that they open with the candidate, and solve together. It would completely short circuit the interviewers that want candidates to give the same answers they would!

4

u/jk147 May 09 '15

I don't think you even need a "sealed" questionnaire. Bring up a problem from one of your projects straight away, maybe a recent problem that stumped you or something that took a bit of work to fix. Work with the interviewee and see how they react to the problem and maybe work with them on it to get their ideas on how to approach it.

This way you can immediately see how the person will work with your team, and a project that actually is something you are currently working on. Instead of some random issue that has nothing to do with what the team is doing overall.

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

48

u/LessCodeMoreLife May 09 '15

I really enjoy interviewing that way actually. I'll pick a largeish looking commit from an OSS project about 10 minutes before the interview and we'll review it together, or we'll talk about how we might add a feature somewhere.

In addition to seeing how you actually work together, it also helps put the candidate at ease to know that you don't have a canned answer in mind. I hate to turn down people just because they don't operate well under pressure. The vast majority of what we do is far less stressful than an interview.

3

u/Zhirgoyt May 09 '15

I'd love to be interviewed by you if nothing else for the fun of it!

→ More replies (1)

8

u/[deleted] May 09 '15

I've had candidates like that too, nothing too extreme though.

That's a great idea! I might steal it... way more "real-world".

2

u/spinlock May 09 '15

When I've interviewed at companies I hated, I'd wait for them to ask me if I had any questions then I'd ask them my own technical question. I've never had an interviewer do well :/

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

14

u/tententai May 09 '15

Exactly. For example problem 5 is trivial with brute force in an "eval" language, and the number of variants to eval is not so huge. This could be a totally acceptable solution depending on the context.

I'd be happy with a candidate who doesn't immediately find the recursive solution but asks questions about this context to know if it's really needed to find a neater solution than brute force.

19

u/epicwisdom May 09 '15

I was under the impression that the recursion was brute force. Just reorganizing loops into recursion doesn't improve runtime directly.

24

u/zeug May 09 '15

Blindly applying recursion in an inappropriate language can result in a performance house of horrors.

For example, this Fibonacci function in C++ looks reasonable:

int fib(int n)
{
    if (n <= 0) return 0;
    if (n == 1) return 1;
    if (n == 2) return 1;
    return fib(n-1) + fib(n-2);
}

but it runs in exponential time with n.

The following runs in linear time with n:

int fib2(int n)
{
    if (n <= 0) return 0;
    int a = 1, b = 1, c = 1;
    for (int i = 2; i<n; i++)
    {
       c = a + b;
       a = b;
       b = c;
    }
    return c;
}

Running fib(45) took ten seconds on my laptop, fib2(45) was instantaneous.

15

u/dagamer34 May 09 '15

It's funny how Fibonacci is the example used for recursion when it's absolutely terrible performance-wise.

→ More replies (15)

13

u/[deleted] May 09 '15

Or it can blow your stack causing your program to die a miserable death.

→ More replies (10)

8

u/curiousGambler May 09 '15

It is. If anything, it'd run more slowly in most languages because of all the function calls.

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

3

u/log_2 May 09 '15

Aside from their ability to code, it's also important to assess how a candidate approaches problems, how they communicate, and how they respond when they're told they're wrong.

I think about the problem in my head and scribble some stuff on a whiteboard. I never lay it out correctly and speak my mind to an imaginary panel when I'm trying to solve something. I guess I would fail at your interview for not uttering any sounds.

The communication aspect is even worse, because you know the answer, so there is no point in both of us trying to arrive at a solution with you pretending to not know.

Programming interviews are not about testing someone, they are about trying to get the candidate to sell themselves. To bad us programmers aren't good at that.

2

u/[deleted] May 09 '15

Well all that said, an interviewer needs to be able to adjust to the candidate too. Again, being strict, saying an interviewee has to conform 100% to any model is bullshit. Removing ego from the interview is tough, but your real goal is to make the company better, sometimes that means finding someone very different than yourself.

Personally I don't ask any "right or wrong" questions - all of my questions have multiple solutions. Part of the interview is discussing pros and cons of the different approaches. Often times I learn new things during these discussions. I don't ask questions that refer to any algorithm or pattern by name. I only ask to see code white boarded if the discussed approach can be knocked out in say 20 lines or less, and only if the candidate seems comfortable with it. If not, next question.

I must see code eventually though. This is where my ego might get in the way a bit. I rarely hire someone who can't demonstrate they are an expert at some language. They can choose whatever language they want, but they must stick to that language, and they can't use pseudocode. It is interesting to see how many people screw up on this part (chose a language they're rusty in, or slip into pseudocode), so I'm pretty lenient on this now.

Anyway, I try to sniff out the strengths of an individual and focus on those. Sometimes I can pick up on them during the non-technical part of the interview. Other times it's while they're white boarding. If this approach were point based, it'd be additive, not subtractive, and there wouldn't be a score limit.

At the end of the day I'd like to think I give everyone a fair chance to display their merits - if they can't then maybe it's not a cultural fit (as opposed to a technical one).

Tl;dr: there's no checklist, no scoresheet, every candidate is a unique snowflake or something.

→ More replies (3)

2

u/dccorona May 09 '15

If you know the answer then they've asked you a bad question. The goal isn't to see how many answers you know, and while it's not bad that you know the answer, it doesn't exactly give an interviewer much information...did you know the answer because you're good and came to the solution that quickly, or because you've seen the problem before and memorized it without ever really understanding it?

You can get around this by throwing twists at them after they've solved the first version of the problem, but it's always more helpful to the interviewer if you explain your thought process to them so they can see how you think. The best questions to ask are the ones that the candidate has no idea how to solve when you first present it to them.

So many people push for interviews to better mirror what you actually working is going to be like, but that's impractical. When you're coding at work, there's an element of blind trust put into it by the rest of your team...for large periods of time they have no idea what you're writing, how you're solving the problem...they have to just trust that you'll come up with something that works well, is testable (and tested), and is maintainable, in a reasonable amount of time.

The interview is to make sure they can be reasonably confident placing that blind trust in you, and for that to be possible, it pretty much by definition can't be reflective of what your standard "work day" is going to be like...even giving you a problem to go off and do on your own and then looking at what you came up with doesn't tell you enough. You need to understand how this person works, and how they're going to function when given a problem they don't know the answer to yet. Just seeing what they come up with, whether given an hour or a week, isn't enough information.

7

u/virnovus May 09 '15

Yeah, when asking someone to solve a programming problem, I'm more interested in seeing how they think then whether or not they get the right answer. Even stuff like what they name their variables can tell you a lot about how someone codes.

26

u/[deleted] May 09 '15 edited May 01 '17

[removed] — view removed comment

→ More replies (8)

2

u/DrMonkeyLove May 09 '15

Exactly. People who demand candidates answer these sorts of questions correctly act like they've never had a bug in something simple they wrote.

2

u/Zhirgoyt May 09 '15

I agree. I have yet to meet a programmer who doesn't make mistakes.

And granted I haven't interviewed for that many positions, but if I were to have an one that consists mostly of some sort of test I'd probably think twice about that work environment either way.

→ More replies (2)

61

u/[deleted] May 09 '15

It's kind of fascinating that even as the industry matures people do not seem to be getting better at giving technical interviews.

my company recently interviewed a friend for an SRE position and they declined saying he couldn't code at all. He worked as a C++ developer for 3 years and was hired pretty quickly at another company where he is writing code full time.

I don't know if he gave terrible answers or not, but I think it's pretty obvious that we were asking the wrong questions.

5

u/[deleted] May 09 '15

The problem is two ways, the interviewer sometimes doesn't ask the right questions (or asks it poorly) and/or the person being interviewed doesn't know to make sure that the right questions are being asked.

7

u/Dank_801 May 09 '15

I recently interviewed for a position at a scrap booking company for a server/web job. The lady that interviewed me had no idea what she or I was talking about. I asked to talk to someone that I could explain my skills to so I could prove that I was qualified for the job (my last ditch effort to try and prove to this lady I was qualified). She said no. So Id have to agree, in an interview I'd much rather do some sort of programming project then have to answer (sometimes very irrelevant) questions. (for kicks, she thought that an item "SKU", you know those numbers that are somewhat universally linked to one item (think best buy), was a programming language). ugh. it was a good job too.

→ More replies (6)
→ More replies (2)

5

u/ethraax May 09 '15 edited May 09 '15

Maybe he's writing shitty code.

I know of people I've interviewed who couldn't code their way out of a paper bag (literally someone with a 4-year degree that had no idea what linked lists were... or hash tables, or trees, or anything), but still somehow got hired as a developer somewhere.

Edit: Also, consider that there's no real programming certification of any kind. Many other technical fields have some sort of certification or license so you can be fairly confident that they can at least perform the bare minimum of their abilities. Programming as no such thing. There are more programming applicants who can't remotely perform the job they're applying to than any other technical field that I know.

→ More replies (6)

0

u/[deleted] May 09 '15

[deleted]

7

u/[deleted] May 09 '15

I know it's kind of a joke, but holy shit does that feel true. Want me to build a scalable PaaS with multiple REST APIs serving thousands of users? Sure.

Want me to whiteboard a meaningless algorithm while 3 people scrutinize my every penstroke? Fuck.

10

u/sizlack May 09 '15

This is it. I'm interviewing for jobs now for the first time in about seven years. I really suck at interviewing now. I just accepted that I'm going to blow the first five interviews I do. So I've been interviewing for jobs I don't want, just to practice and get warmed up for the "real" interviews. It's not fair to those companies because I'm wasting their time, but I see no way around it.

→ More replies (3)

38

u/Number127 May 09 '15

I suspect many good programmers could find a working solution to #4 and #5 within an hour. However, I think very few could find a solution in that time limit and be confident that it's guaranteed to produce optimal results, or to do so in a reasonably efficient way. And, as we see from the original author, false positives are also to be expected. All of those factors make this a very poor interview exercise.

12

u/Lobreeze May 09 '15

Read the guy's About page and check out his website. The guy clearly has his head up his own ass.

43

u/SlobberGoat May 09 '15

Real programmers skip over egotistic top-10 'programming fashionista' bullshit.

2

u/[deleted] May 09 '15

I don't think that an article extolling the frustration that you have to interview people calling themselves programmers when they can't program at an appropriate level is "fashionista".

26

u/IAmDumbQuestionAsker May 09 '15

Isn't the problem with obsessing over these questions for whiteboard coding is that people are just going to drill themselves with CTCI and Programming Interviews Exposed and other similar books until you get people who can breeze through whiteboard interviews but don't actually know how to code in real world situations?

It makes as much sense as evaluating applicants for college solely based on standardized tests.

21

u/greg90 May 09 '15

Right, when I get interviews that are loaded with these types of questions I consider it a red flag not to work for the company because it says something about their attitude and values.

5

u/[deleted] May 09 '15

In the past, I have been asked to solve a programming puzzle on paper, written out by hand by one of the employees. I made it clear I do not program without a qwerty keyboard and a computer.

3

u/greg90 May 09 '15

I don't know, I write lots of code on whiteboards during interviews, I found no way to avoid it.

→ More replies (4)
→ More replies (24)

20

u/staticassert May 09 '15

I feel like CS is so split between people with crippling impostor syndrome or people with ridiculous egos.

3

u/[deleted] May 09 '15

CS? Sounds like IT/anything computer-related in general.

→ More replies (1)

27

u/halifaxdatageek May 09 '15

Moral of the Story: Humility is a virtue for a reason.

15

u/0xbitwise May 09 '15

This idea that every "true programmer" will get the correct answer to some arbitrary set of pet questions becomes increasingly unlikely as the difficulty and rarity of the problem increases. I'm sure every veteran programmer here has their own pet problem that's super difficult to solve without experience.

I think this is a symptom of the real problem, which is that many other programmers, often responsible for hiring, assume that you can take someone's knowledge of the ins-and-outs of programming for granted if they have rigorous experience in generating algorithms for puzzle problems.

Knowing time and space complexity is a must for writing great code, but that's only the bare minimum. What about asking them to demonstrate proper code practices? (This applies to more mature languages and frameworks, I wouldn't ask someone to demonstrate "best-practices" for Node.js apps, when the whole damn Internet can't seem to agree on what that is, yet.)

What about proper UX? Do they know the value of throttling and debouncing inputs? Creating UIs that inform the user not just that something IS loading, but what in particular is loading? Do they know the value of telling the user a technical step in the loading bar, even if he/she has no clue what that means?

Do they know how to properly implement logging? Do they know when to catch exceptions or re-throw them (to prevent different layers of the application swallowing exceptions from layers beneath)?

These are all of the things that you'll probably encounter on a daily basis with any sort of application you write and maintain, yet I have never once been asked in an interview to explain my methodologies for this sort of thing.

Complexity is something you should definitely test with a more generic problem or two, but creating ridiculous problems with gotchas and edge cases is more of a test of whether or not someone was lucky enough to encounter a similar-enough problem; not their ability to write good code.

NOTE: If your responsibility is to write incredibly complex algorithms as a part of your work in a position, then having more complex tests of their algorithmic rigour is much more important, and I would hope that a candidate would expect that going into the interview.

27

u/TOASTEngineer May 09 '15

Mind, they're good questions to ask. The key is to look for an answer, not a right answer and definitely not one 'right' answer. You want to observe that the applicant can solve programming problems, not just that they've memorized the answers to a handful of common interview questions.

→ More replies (10)

11

u/mike413 May 09 '15

Damn, now the CEO of Zenefits will withdraw his offer too, ppl!

→ More replies (2)

11

u/cmcpasserby May 09 '15

How is being able to solve whiteboard questions with no resources useful? Programming is as much about learning and research as it is about logic.

I rather see how a applicant approaches learning something completely new, and how he applies those newly learned skills.

→ More replies (2)

14

u/mp2146 May 09 '15

He's since updated with the following two gems:

A bunch of people got up in arms because exercises like this shouldn't be asked in an interview. I disagree.

And then:

For some reason (I'm blaming my asshole-ish tone and the cartoon accompanying the post), people assumed that these 5 problems were meant to be solved during an interview.

I didn't try to say that, but I see how you could easily misunderstood the post. My bad. The idea is that you should be able to solve these problems in less than 1 hour, but I didn't mean that it had to be done during an interview.

3

u/scribble_child May 10 '15

He should just stop digging - pull the post, and stay off the internet for a few weeks.

→ More replies (1)

17

u/argv_minus_one May 09 '15

With sets that small, you may as well just brute-force it. Try every combination of the provided inputs and find the one with the highest magnitude.

Here's a Scala one-liner that does just that:

def solve(l: Seq[String]): Int = l.permutations.map(_.mkString.toInt).fold(0)(math.max)

11

u/_cortex May 09 '15

This is exactly what I would say in an interview. The easiest possible answer and certain to produce correct results. If it later turns out that the input set is large enough to make this slow, you can always go back and think of a more complicated but faster solution.

11

u/dccorona May 09 '15

(Looks at the posted solution to #5) - being good at algorithms is great and all, but if you code with untyped generics (ArrayList instead of ArrayList<Integer>, or even better List<Integer>) then you should probably stick to the conceptual work.

6

u/scalava May 09 '15

Yeah generics were added in Java 5, you have no excuse to be using untyped collections now.

20

u/[deleted] May 09 '15

Is there someone on reddit that works (or used to work) with svpino?

Would like to know their experiences working/developing with him.

→ More replies (26)

43

u/random314 May 09 '15

I can't believe the amount of people who underrate the ability to solve a little bit of problems. I mean I understand problems like 4 and 5 can be a bit excessive, but 1-3 is pretty basic for ALL programming jobs (yes entry level included).

At the very least a simple thinking problem should be asked at all programming interviews. If you can't sort / sum / reverse a list of integers, you're wasting my time.

75

u/RICHUNCLEPENNYBAGS May 09 '15 edited May 09 '15

True but why do people keep writing the same article over and over again? Did the original post really add anything new to the discussion that the original Fizzbuzz post from however many years ago, and the countless other almost identical articles, didn't?

I mean I suppose the reason they get so much traction is they push people's buttons and everyone feels like they need to check it out and see if they can easily solve the problems.

25

u/[deleted] May 09 '15

The blog author is one of many such blog authors who churn out their own re-worded version of other people's blogs. I've seen him spamming his blog here before. I'm thinking of blogging about my own personal hiring heuristic which states that people who blog about how to weed out poor hires by using techniques already blogged to death about are far less capable than they believe themselves to be. But then of course I myself would be subject to that heuristic. It's a bitch.

→ More replies (2)

2

u/a_simple_pie May 09 '15

I'm only a 2nd year Software engineering student and I thought the questions asked were all super simple. The problems were all set up so even the worst implementations could be feasible in terms of time and space. I would totally understand if an applicant got rejected for not being able to logically think through the problem and take an educated stab at it.

→ More replies (30)

96

u/BlackDeath3 May 09 '15

Is there any purpose to this post beyond further ridiculing that blog author?

182

u/raydlor May 09 '15

I think, given how critical he was in the blog post, it's somewhat appropriate.

42

u/givanse May 09 '15

Right, measured by his own ruler (or however the saying goes in English).

419

u/[deleted] May 09 '15

No, but come on, it's hilarious.

10

u/heroOfTimeBitch May 09 '15

it's like instant karma

→ More replies (72)

92

u/prelic May 09 '15

I already said it in another comment, but I think the reason this thread even got created is because the original blog post was so arrogant and condescending, that it was pretty funny that he messed one up. I mean, the dude literally said "if you can't solve these, change your resume because you're not a programmer. Find anyone you think is dumb and challenge them to solve these problems".

→ More replies (4)

6

u/4forpengs May 09 '15

A few more words about the previous post

A bunch of people got up in arms because exercises like this shouldn't be asked in an interview. I disagree.

As long as there are no hidden tricks, or special knowledge involved, or any other gotchas, I think these are perfectly valid questions to ask.

I never said that you'll be hired if you know how to answer these problems, but I won't consider you if you can't.

Everyone has a way. This is mine.


Those are this author's words. He technically wouldn't even consider himself for the position.

Now, whether or not you decide to ask these questions on an interview is up to you. However, if you can't solve your own interview problems, that's a problem of it's own.

The guy is so condescending and on such a high horse, that he thinks he is beyond himself.

3

u/BlackDeath3 May 10 '15

I don't think this answers my question.

→ More replies (1)

39

u/NoMoreNicksLeft May 09 '15

I'd certainly like to discourage the concept that you can test for "programmer-icity" with what amount to stupid riddles.

"Aaaaaaaaaaand what! is your favorite color?!?"

An interview is almost certainly the most stressful situation a person will ever be in that doesn't risk actual death. You'll never truly see potential by throwing these dumbass fucking tests, nor can you really uncover any of the other personality flaws that might make someone unhireable.

They exist because a certain class of middle managers like to think they're more clever than they are, having read all the management books you see on their shelves, and so they make up some tests ("if she weighs the same as a duck!") that don't actually have any empirical backing at all.

Has anyone ever done a study of the productivity/quality/creativity of the code of people selected by succeeding at these tests, vs. those who failed them (and the hiring process)? If no one has, why should any sane person believe that the tests have any validity?

6

u/jimmpony May 09 '15

An interview is almost certainly the most stressful situation a person will ever be in that doesn't risk actual death.

This is ridiculous hyperbole. There are far more stressful non-lifethreatening situations than a coding interview.

→ More replies (10)

4

u/dccorona May 09 '15

I don't know if there's external ones, but Google, Microsoft, etc. almost certainly have all kinds of internal studies around their interview processes (after all, Google changed theirs because of research they did). People sit on reddit and complain about interview processes like that all the time, but ultimately all they have is their own anecdotal personal experience, which isn't worth much compared to massive companies who have spent millions auditing their interview processes and have decided to do it this way.

2

u/chriswen May 10 '15

"Aaaaaaaaaaaaaand what! is the capital of Assyria?!?"

→ More replies (32)

5

u/phatrice May 09 '15

Never let a good karma farming go to waste, not even on /r/programming

→ More replies (4)

6

u/CaptainParanoia May 09 '15

Real programmers don't make inane blog posts.

8

u/AlSweigart May 09 '15

Nobody in tech knows how to interview.

2

u/NakedNick_ballin May 09 '15

And nobody outside tech knows, tech

2

u/MpVpRb May 09 '15

Nobody in tech knows how to interview

Nobody knows how to interview..IN ANY FIELD

Well, maybe some do, but they are rare

3

u/Jukebaum May 09 '15

We had the fibanocci numbers question in our cs course a few weeks ago. I thought it was entertaining because it is a puzzle to solve it instead of a real problem. There is basically a "trick"(vast overstatement but for a beginner it is exactly that) to that problem to solve it fast. We had then to merge it with a runtime counter to compare the time need to produce these numbers between a linear approach and a recursive one.

If solving this stuff makes me a worthy applicant .. Damn I am golden :D (which I highly doubt)

7

u/heroOfTimeBitch May 09 '15

Oh man this is so funny, the douchebag got douche'd

5

u/AnswerableQuestion May 09 '15

Most hiring managers wouldn't hire themselves for the job.

3

u/robertDouglass May 09 '15

I know I wouldn't! But that doesn't mean I can't hire good programmers.

5

u/skewp May 09 '15

I was pretty sure on my initial read of the problems that he was greatly underestimating the difficulty of #4.

2

u/[deleted] May 09 '15

Most here aren't any better, almost everyone talks about how 4 is more or less obvious and then they post a solution that isn't even close to working. If you think that 4 is easier than 5 then you are most likely wrong about 4.

2

u/kirbyfan64sos May 09 '15

Hey, he said you had to solve it in less than 1 hour, not that it would work the first time.

2

u/[deleted] May 09 '15

ok I just look into the commends and I see the "you ain't gonna need this ever" blah blah, I fully agree but is there somebody that actually did solve to problem?! Is there somebody that sat down just for his love for problem-solving and is able to hand me an algorithm? you are not any less douches if you just keep bitching about how much of a douche the blogger is.

2

u/[deleted] May 10 '15

you think its flowery bullshit? whoever thinks this is not a programmer then...

4

u/bacondev May 09 '15 edited May 10 '15

If I was given the fourth problem in an interview, I'd roll my eyes. If I was given the fifth problem in an interview, I'd be pretty aggravated. It's not that I can't do them, but let's be honest. It's in a programmer's nature to be lazy. I don't want to have to break a brain sweat during an interview. I'd rather have a calm and relaxing interview—one that is efficient in acquainting the interviewer with me and vice versa. This might give the interviewer an idea of how my brain works, but they could more efficiently just ask me for the general algorithm and to discuss it than to ask me to write a working solution.

3

u/MikeSz May 09 '15

good fun doing the exercises. only number 4 gave me trouble - brute forced it with the permutations library, so there is probably a better way.

3

u/hpp3 May 09 '15

Sort the numbers in reverse, as strings. You need to also pad all the strings to be the same length in order to compare, by repeating the string until it's the right length. E.g. 123 padded to 7 digits would be 1231231.

6

u/[deleted] May 09 '15

This doesn't work since it would see 56 and 565 as equal but they aren't. If you solve it this way then you need to pad till their least common multiple length.

→ More replies (1)

10

u/elcapitaine May 09 '15

See the author's solution - they originally thought the same thing, but that's wrong. It doesn't work for all cases. https://blog.svpino.com/2015/05/08/solution-to-problem-4

I never said that you'll be hired if you know how to answer these problems, but I won't consider you if you can't.

Guess they should fire themselves. Problems like these are fine - I liked solving this one, but if you're looking for the right answer as opposed to trying to see how the interviewee approaches problems you're interviewing wrong. This author comes across like an ass, and I'd never want to work for him.

→ More replies (3)

2

u/Kaligraphic May 09 '15 edited May 09 '15

I did it a lazy way. I copied the numbers into a second array, but converted to floats. For each value, while that value is >10, I divided it by 10. That gave me single-digit numbers that I could sort.

Then I sorted the copy, but with a twist - I did the comparisons on the second array, but I did the moves on both arrays. This effectively sorted both arrays by the highest first digit. (I just did a bubble sort for proof of concept and because I'm a lazy git who normally uses Array.Sort() or equivalent, but a quicksort or better is fine too.)

then I spit out the string representations of every value in the first array, in order.

Then I realized that 9.1 sorts before 9, when I should put the 9 first. At this point, I realize that even if I had used a padding trick, 910 sorts before 900, so it would have been the same result, and I ended up concluding that brute force may actually be the only workable option.

→ More replies (7)
→ More replies (3)
→ More replies (5)

3

u/surlysmiles May 09 '15

Whatever. Don't piss on him. I'm sure he's probably a good programmer. Just focus on bEing the best you can be. I personally thought the problems were enjoyable.

3

u/_exalt_ May 09 '15 edited May 09 '15

my oneliner: print(max([int(''.join(x)) for x in itertools.permutations(['56', '50', '5'])])) not efficient nor practical but i solved the problem in 5 seconds

→ More replies (1)

3

u/[deleted] May 09 '15

They must be one of those imaginary programmers that are so common these days.

3

u/Dank_801 May 09 '15

Couldn't you solve this very easily by looping through every possible arrangement of values given by said numbers? Then at the end of every loop you could just check to see if the new number was greater than the old (previously biggest) number?

I mean sure it wouldn't be efficient at all. but its a simple way to solve the problem...

→ More replies (2)

3

u/chaszzzbrown May 09 '15

What I find lacking in most of the solutions given is any sort of proof as to why the algorithms will correctly yield the answer.

What mostly seems to be provided is some heuristic approach: "Well the bigger the first digit is, the better". Or else saying "well, it also handles tricky case like [gives some examples]."

I mean, why does sorting pairs a,b via (a+b)>(b+a) yield a correct solution, when sorting pairs a,b via a>b fails?

There is a proof that the former works (an optimal solution must be linearly ordered according to the given comparison function; and all linear orders produce the same result); but it is all too often that we as programmers don't or can't formulate answers to these types of problems in such a way that we can be sure that their algorithms work the way they suppose it will.

5

u/sitdownstandup May 09 '15

I thought the questions were fun, as did others. That's why it was voted to the front page.

6

u/Synovius May 09 '15

IT professional here who does web development for a living and tech screens candidates for our company.

Nobody will ever use questions like these. The questions that will be asked during technical interviews are about relevant experience to the technology and your ability to develop on it. That's it. You might get one logic question but questions like these tell you nothing about the ability of the candidate with the technology your company works with.

It's the same problem I have with colleges and universities who, during Computer Science undergraduate, pound you with abstract, flowery bullshit about binary search trees, sorting algorithms, performance calculation, and other somewhat useless information instead of actually teaching you to do real things and create real solutions.

6

u/RoboticElfJedi May 09 '15

The questions that will be asked during technical interviews are about relevant experience to the technology and your ability to develop on it.

In my experience hiring developers, you will get a lot of people with ok resumes and have been working ("working") at another company with the platforms you are looking for, who couldn't solve these problems. With hindsight, I should have used a few things like this for screening. Even "write a for loop that adds the integers from 1 to 10" would save a lot of time.

2

u/speedisavirus May 09 '15

"write a for loop that adds the integers from 1 to 10"

Had someone that couldn't do this. Despite my huge protest still hired and still the biggest shit stain on the team.

→ More replies (3)

9

u/[deleted] May 09 '15

Comp Sci undergrad here.

My guess is the reason as to why we're being force-fed all the "abstract, flowery bullshit" is because, at least as far as building code and software systems, that carries more weight than "can you work with technology x or code in language y," right?

Granted, being able to actually think on your feet and actually build solutions from the simplest algorithm to the most complex network of systems is the ultimate goal, the way you put it makes it sound rather...oversimplified, I guess. If it's simply "how long have you worked in environment x," couldn't every Tom, Dick, and Harry who's dabbled with it for a few years independently be in IT/software engineering/systems management to some capacity?

→ More replies (4)

8

u/Fenix42 May 09 '15

I live in a college town. I am also a dev. Even the interview for senior dev jobs at MAJOR companies (like Amazon) have these type of questions. Shit some have WRITTEN tests. Like you would get in class. X(

3

u/[deleted] May 09 '15

I was asked far more difficult questions for a junior role at Amazon. Please, don't act surprised by this just study and learn your principle concepts.

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

15

u/erebuswolf May 09 '15

Person who has worked for Adobe, Nvidia, Google, and Boston Scientific as a programmer here. You WILL be asked questions like this but generally harder and expected to program a solution on a white board. Don't join a company that doesn't actually force you to program as part of the interview process. If they aren't ensuring every person they hire can actually write code there is a good chance there are people they have hired who can't.

2

u/[deleted] May 09 '15

To be fair: 'programming during the interviewing process" is a very different statement than "programming on a whiteboard", programming on a whiteboard is shitty and doesn't show you somebody's abilities properly. This kind of tests aren't terrible, but I would rather send it to someone to do at home instead of making them write code under unnecessary pressure.

Google is a big company but their hiring process is less than stellar, I believe problem-solving skills, critical thinking, and being resourceful are much more important than remembering specific stuff, specific knowledge is easier to gain.

→ More replies (1)

4

u/[deleted] May 09 '15

This is what makes me sad. This is the state of affairs nowadays. I'm a web developer but came from the traditional CS route and it blows my mind seeing people who want to cry how this is so "unfair" and "useless". This isn't fucking torture nor is it rocket science and my guess is the people getting most upset are the ones most challenged/incapable when it comes to solving these problems.

Is it seriously so goddamn much to ask that the potential programmer the company would pay decent money to be able to, you know, actually program? I hate that web developers especially think they get some pass when it comes to this, and it's why people view web development as some kind of "toy" career - because so many people don't take the time to learn their shit and it makes those of us that do know our shit look bad.

3

u/gnuvince May 10 '15

There was a story a few weeks ago: http://www.thousandtyone.com/blog/EasierThanFizzBuzzWhyCantProgrammersPrint100To1.aspx

Basically, the more someone whines, the more time it takes them to solve the problem. I think that a lot of the people complaining about the problems here are people who have difficulty solving them. Again, I'll reiterate that I find the problems to be pretty good (great even), especially #4 and #5, since they are not completely trivial like FizzBuzz, have many solutions from brute force to more elegant, test your ability to find and reason about edge cases, and most importantly, are a great way to open a discussion with the interviewer.

I see many people saying that these things never happen in the real world; these exact things, perhaps not, however you will have problems where dealing with what happens with zero elements or too many elements can cause a problem, you will need to think about whether your brute force solution will scale when you have double the users (web scale anyone?), you will need to talk with other people on the team about your solution, explain why you think it works, etc. A good interviewer can learn a lot about a candidate from these simple puzzles, and a good candidate should be able to use his performance on these puzzles to identify possible weaknesses and seek to address them. Saying "this never happened to me in 5 years of writing CRUD Rails applications" helps no one, least of all you.

→ More replies (2)

3

u/[deleted] May 09 '15

We're a .NET shop. We had one question for candidates for "Mid-Level .NET Developer" - we gave them a laptop with the screen projected on the overhead screen in the conference room and nothing open but a SQL connection string in notepad and the Visual Studio IDE.

The "problem" was: connect to the database and display, in either a Windows Forms or a Web Page, the data contained in the only table in the database. You can use Google or any resource on the internet. We will not hold your methodology against you, and in fact we like to see how resourceful you are.

We eliminated 7 of 10 candidates who could NOT perform this task.

→ More replies (1)

3

u/[deleted] May 09 '15

Off topic, but personally I think challenges like these are dumb. You will more than likely never need to program such a method and it doesn't teach you anything worthwhile for the Enterprise

→ More replies (3)

3

u/[deleted] May 09 '15

"Real programmers don't use Java."

→ More replies (1)

2

u/[deleted] May 09 '15

Yup, don't regret calling this guy a douche bag at all. He just keeps adding more evidence that he is

2

u/08TangoDown08 May 09 '15

Mmmm yes, I'd love a second helping of that delicious irony!

→ More replies (1)

2

u/k1n6 May 09 '15

That's an np complete problem..... There is no computationally "fast" way to solve it

2

u/[deleted] May 09 '15

What, programmers are not allowed to be human and make mistakes? Is it really news worthy when an expert programmer makes mistakes?

8

u/[deleted] May 09 '15

It is when he states:

I never said that you'll be hired if you know how to answer these problems, but I won't consider you if you can't.

So he wouldn't hire himself.

2

u/boner79 May 09 '15

TIL people on these programming subreddits are arrogant as hell.

2

u/MadLibz May 09 '15

Such a real programmer that he uses Java. swoons

2

u/mk_gecko May 09 '15

Real programmers understand the "no true Scotsman fallacy"

2

u/[deleted] May 09 '15

So you are not a real programmer? :)

3

u/mk_gecko May 09 '15

haha. No, the person who posted these questions would not be a real programmer ... oh crap. circular logic [abort][core dump]

2

u/astronoob May 09 '15

I fucking hate coding questions in interviews. I refuse to take them when I'm asked to perform one and I vehemently object when a company I'm working at uses them. A code challenge only makes sense when the person you're interviewing does not have the adequate experience, education, or references you're looking for.

3

u/robertDouglass May 09 '15

As a hiring manager, I agree with yocheckoutmyaltcodes, and the written stuff is only a means of securing the interview for me. What goes down during the interview makes all the difference, and I am looking for evidence of curiosity, problem solving skills, and reasonable assurance that you are not an axe murderer.

→ More replies (3)

0

u/[deleted] May 09 '15

[deleted]

→ More replies (2)

1

u/[deleted] May 09 '15 edited May 09 '15

Anyone care to evaluate my ideas? I know Java ok.

for number 4: numbers in a higher place will always beat numbers below them. So you want your first sequence of numbers to be as high as possible, then after that it matters much less. Putting a 90210 in front of an 8 should always be worth it, because Of the lead digit.

Declare an int array length [number of digits], populate it with the numbers then sort it by lead digit (find and place all the 9's, etc. After that is done, stick the numbers with a tens place or above in their proper place (after the smaller numbers mind you. If you're comparing 9 and 92, 9 wins. 9009 and 92, 92 wins. .hasnext() is my friend) using more loops that sort them by iterating across the length of each variable (representing the integers) comparing digits of each number by modding for that tens place. After that is done it should be ready to print with a loop.

For 5:

brute force time, cause time limit. make a version for each variant, every combination of the operations and numbers. Compare them to Parallel universes, to make them sound cool. Number1 (+, -," ") number2 ... etc. That branches too, so

have one huge ass 2d string array declared, [ length of the number lis, plus that-1 3 ] [length of number list * 2 -1]

evaluate each combination (iterate through each of the first index) of num1 (+,-," ") num2 etc... by switching over the value of the char at the proper index in the string array, evaluating either a + b, a - b, Or a * 10 + b based on the appropriate character in a loop, summing up results. If it's the correct number move on, if it's the wrong one set the index to null and move on. At the end print all the nonnulls and you're done.

I forget to type things when drunk. I typed out my attempted solution method in words beforehand this time, instead of half assing it from memory

→ More replies (3)

1

u/vps91 May 09 '15 edited May 09 '15

This compare function should work also, lhs should be placed before rhs if (V1+V2).toInt is greater than (V2+V1).toInt More formally, int val1 = Integer.ParseInt(v1+v2); int val2 =Integer.ParseInt(v2+v1); return val2 - val1;

1

u/Doriphor May 09 '15

Padding is a good idea but you need to pad numbers with their last digit not 0. (Wait, does that work?)

→ More replies (2)

1

u/Calvinatorr May 09 '15 edited May 09 '15

I think the problem is a bit ambiguous, because the solution to me is assuming the list of unsorted integers is actually a list of strings that are being compared, but I read it as simply being a list of integers that need to be sorted into a list to give the largest possible number as a string - so you would sort by descending order and then iterate through the newly sorted list and append each number as a string after converted said number to a string.

1

u/thardoc May 09 '15

this stuff is scary to me as a guy who just completed freshman year of CS. "Real programmers can do these easily". I can do the first three pretty easily, I could probably figure out the fourth - but the fifth I'm not sure how to approach besides brute force, and even then its tricky to me . I doubt I could solve all 5 in under an hour...

2

u/[deleted] May 09 '15

First 3 seemed reasonable filter for those who can't code.

The last 2 seemed quite complex and time consuming in some languages I would probably like to use like C...

→ More replies (2)
→ More replies (4)

1

u/Geemge0 May 09 '15

The update solution just stringifys and compares.... Quick and robust but not very efficient at all. I'd implement this STUPID question in the same way.

1

u/Noneatme May 09 '15

I can do that, here hold my hard drive

1

u/halifaxdatageek May 09 '15

I feel like I should summon /u/svpino to give their side of the story.

2

u/defnotthrown May 09 '15

we had his opinion which is "I'm still smarter than you"

Upon realizing how ridiculous he made himself sound he deleted that and moved on to "I'm sorry, the internet hurt my feelings, so I wanted to hurt it back".

1

u/lorddarkflare May 09 '15

I can do 1 - 4 in about 8 -10 minutes. 5 would most likely take the rest of the hour.

Unlike the others, you actually need to think through a solution as it is not super obvious.

1

u/glenbolake May 09 '15

He even gets really close at the end. He says you can pad with the last digit, so 51 becomes 511. You should be padding with the first digit. This was my implementation, and it works:

def find_largest(numbers):
    import string
    number_strings = sorted([str(x) for x in numbers], reverse=True)
    big_digit = number_strings[0][0]
    contenders = [x for x in number_strings if x[0]==big_digit]
    length = len(contenders[0])
    padded = [string.ljust(x, length, big_digit) for x in contenders]
    return contenders[padded.index(max(padded))]

def biggest_number(numbers):
    rv = ''
    while len(numbers) > 0:
        big = find_largest(numbers)
        numbers.remove(int(big))
        rv += big
    return rv

def brute_force_biggest_number(numbers):
    from itertools import permutations
    return max(''.join(map(str,p)) for p in permutations(numbers))

if __name__ == "__main__":
    print biggest_number([42, 420, 423])
    print brute_force_biggest_number([42, 420, 423])

(I'm relatively new to Python, so I'm willing to accept critique)

2

u/chaszzzbrown May 10 '15

try [51,51519]. Should get 5151951; looks like you get 5151519.

1

u/Phildos May 09 '15 edited Nov 12 '25

serious plough march door knee detail degree rob chunky juggle

This post was mass deleted and anonymized with Redact

→ More replies (1)

1

u/aek82 May 09 '15

Couldn't do it in four hours, but the general algorithm I came up with in C# is:

  • Compute all permutations of the list.

  • Add all permutations into a List object and order by descending.

  • First element should be the largest combination.

Gist

1

u/[deleted] May 09 '15

If I wanted more of these kinds of problems to work on as a problem solving exercise where might I look?

→ More replies (3)

1

u/gerrylazlo May 09 '15

I spent an hour at work coming up with a solution to #5 in excel using vba and calculated cell values. It would display all the correct answers as it whirred through the possibilities. It was the most fun I had all week at work and reminded me why I loved programming in the first place.

1

u/[deleted] May 10 '15

The author mentions padding solutions, but not the actual correct one. If you pad the strings with the most significant digit instead of the least, you can qsort to victory.

Hokey C solution I just crufted together in a couple minutes. You can pass n values in as arguments on the command-line and you'll get the biggest number.

2

u/vidude May 10 '15

Counter-example: 56 56554 56556

The correct answer is 565655656554, while your algorithm would give the answer 565565656554. I don't think you can escape doing some level of brute force analysis. The perl script below starts with a lexically ordered list but recursively tries all different orderings of numbers that have the same starting digit. Of course it degenerates to full brute force if all numbers start with the same digit. Dynamic Programming techniques could be used to improve this.

#!perl
use strict;

sub removeNthElement($@) {
  my $remove = shift;
  my @list = ();

  for (my $i = 0; $i < @_; $i++) {
    if ( $i != $remove ) {
      push @list, $_[$i];
    }
  }
  @list;
}

sub calc($@) {
  my $so_far = shift;

  if ( @_ == 1 ) {
    # only one number left, append it and we're done
    return $so_far.$_[0];
  }

  # calculate how many numbers at the head of the list have the same first digit
  my $n = 1;
  while ( $n < @_ && (substr($_[$n],0,1) eq substr($_[0],0,1)) ) {
    $n++;
  }

  # first let's assume we will use the next number
  my $value = calc($so_far.$_[0], removeNthElement(0, @_));

  # now let's see if we can do better with one of the other choices
  if ( $n > 1 ) {
    for ( my $i = 1; $i < $n; $i++) {
      my $alt_value = calc($so_far.$_[$i], removeNthElement($i, @_));
      if ( $alt_value gt $value ) {
        $value = $alt_value;
      }
    }
  }

  return $value;
}

my @list = qw/56 56554 56556/;
print calc("",sort { $b cmp $a } @list)."\n";
exit;
→ More replies (3)

1

u/realCommentor Jun 03 '15

Based on my limited experience writing software for fun, in my free time, by myself, I highly doubt that there's any significant correlation between rapidly solving programming trivia in an interview setting and designing complex, real world software. At the beginning of most real software projects (again, in my limited experience), there's little more than a basic, misguided understanding of how to proceed. Over time, an understanding of the design is slowly gained as several attempts (mostly failed) at solving it are made. A good design is often the result of the numerous failed attempts that are needed to gain a solid understanding.

It might be fun or interesting to solve the five programming problems mentioned here. There's probably even some benefit in doing so. But on a real project, there's never the luxury of even knowing what those five problems are, or if there even are five, or five hundred, or whatever. To even get to the point of (presumably) knowing that there are five problems is almost the entirety of the challenge, to the extent that the implementation of those five problems is just a trivial matter.

It may very well be that there aren't any good ways of finding "real programmers" based on interviews. I don't know. But asking the kinds of questions mentioned here seems like a huge disservice to the profession. If I was hiring a builder to construct my house, I wouldn't ask him/her to hammer some nails into a piece of wood as I stood there with a stopwatch. Such a test says no more about the builder's ability to build a home than the five programming problems here say about a programmer's ability.

1

u/bakuretsu May 09 '15

I think anyone who is presented with this problem and who immediately reaches for a Java compiler to solve it is a masochist.

1

u/greenthumble May 09 '15 edited May 09 '15

3 was easy but I had never coded it up before so decided to take a look at how I would do it in Clojure. My solution was alright, basic top down type recursion that built up the array at each step with a little extra trick called "trampoline" applied so the function doesn't get all stack-smashy and using the +' operator which uses BigInt. Then I saw this:

http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci#Recursive_Version

Which leads directly to this:

http://en.wikipedia.org/wiki/Corecursion

Wow. I've been doing this a looooong time and Clojure keeps teaching me new things. Like there's a whole different way to think about recursion.

That Clojure example is doing the extremely neat trick of defining the array in terms of itself - the array equals the array plus the array shifted (using "rest" to shift, self-referentiation, and lazy evaluation).

Edit: Oh yeah heh also #1 and 2 are ridiculously simple in Clojure:

(apply + [ 1 42 8675309 ]) => 8675352 ; #1 (for and while makes no sense here)

(interleave [ :a :b :c ] [ 1 2 3 ]) => (:a 1 :b 2 :c 3) ; #2

Edit2: Oh right and if lib func interleave is cheating there's also a basic sequence manipulation way to do #2:

(mapcat vector [ :a :b :c ] [ 1 2 3 ])

2

u/LazinCajun May 09 '15

It is really cool! There are applications of basically the same idea to dynamic programming in lazy functional languages too.

→ More replies (3)

1

u/Reddit1990 May 09 '15

Well that's fucking funny. Nice one, man.

1

u/egg1st May 09 '15

Falls down if you have a set like [5, 53, 50, 4, 9], because the concatenation of 5 & 4 will give you a higher number than 53. This code gives you 9535054, but the correct answer is 9545350.

5

u/NVShacker May 09 '15

Isn't the correct answer 9553504?

→ More replies (1)

2

u/Doriphor May 09 '15

The correct answer for [5, 53, 50, 4, 9] is actually 9553504...

→ More replies (1)

1

u/code-master May 09 '15

You will never get a job if you will write such poor code regardless of its correctness.

5

u/[deleted] May 09 '15

What do you think the author should have done instead?

1

u/Xuzon5 May 09 '15

It is curious I've done 4 of these 5 problems in the first two weeks programming in C in my college

1

u/code-master May 09 '15 edited May 09 '15

One of the solutions avoiding string concatenation:

When you've got number with 5 digits 15111 and number 1 [15111 1] you must append 4 missing digits from first number at the end of second number taking them from left to right, so e.g. : 15111 would be compared to 11511 (1xxxx) and then its decided to switch their positions or not.

eg. [50 507] becomes [50x 507] -> [505 507] and because 507 is greater than 505, 507 is placed at the front, so result is 50750

lets test it against some input:

[10 5] -> [10 5x] -> [10 51] (swap)-> 5,10

[51 5] -> [51 5x] -> [51 55] (swap) -> 5,51

[56 5] -> [56 5x] -> [56 55] (no swap) -> 56,5

[55 5] -> [55 5x] -> [55 55] (no swap) -> 55, 5

[95 9] -> [95 9x] -> [95 99] (swap) -> 9,95

[15 1] -> [15 1x] -> [15 11] (no swap) -> 15,1

[11561 1] -> [1151 1xxxx] -> [11561 11156] (no swap)

[9998 9] -> [9998 9xxx] -> [9998 9999] (swap) ...

I don't know if this method is more efficient than plain string concatenation, but it works. Something tells me it is. If digits were converted to BCD (binary coded decimal) it would be easier to do comparisons. Or just convert to readable hex, e.g. 999 -> 0x999 and do the math.

I think nobody came to such original solution like this, so from the whole crowd here I would hire only myself.

Still, this is not a real-life programming problem.

→ More replies (14)

1

u/chasevasic May 09 '15

Many people are saying that this wouldn't be good for an interview (that is not to say that these types of question are uncommon in an interview!)

It doesn't require you to:

  • document your code

  • write tests

  • practice good architecture techniques & code style

  • use revision control

  • use (possibly complex) build systems

  • communicate with a team

  • follow a schedule

  • probably other things not in the front of my mind

5

u/Choralone May 09 '15

Yes,but if you can't write simple code, the rest doesn't matter.

3

u/chasevasic May 09 '15

Agreed. It is a sad state when people who can't even code are trying to get jobs doing it though. I don't think that happens so much in other industries... I don't want to get a job where I'll have no clue and screw everything up...

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

1

u/MattRix May 09 '15

Upon reading the original 5 questions and attempting solutions for them, I was thinking "either I'm a bad programmer or #4 was way harder than he realized", which is why I asked him to post his solution: https://twitter.com/svpino/status/596697914895945729

Feeling pretty smug now :D

1

u/MpVpRb May 09 '15

Writing real, working code is hard. Writing ABOUT code is much easier

It's very common to see errors in documentation, tutorials and examples

And, it's not just in computer code

Cooks Illustrated regularly tests published recipes looking for errors

Errors in recipes are FAR more common than you might imagine. Good cooks automatically correct and adjust, while newbies wonder "why can't I get this right"

1

u/senatorpjt May 09 '15 edited Dec 18 '24

fretful axiomatic onerous late plant somber existence wine shelter overconfident

This post was mass deleted and anonymized with Redact

1

u/mikemaca May 10 '15

It's very similar to Muphry's law whereby all grammar and spelling complaints contain errors. Likewise claims about programmer competency must always include buggy code as solutions.

0

u/[deleted] May 09 '15

[deleted]

→ More replies (432)

0

u/[deleted] May 09 '15

[deleted]

→ More replies (90)