News for hackers. Stuff that matters.
Re:Data structures and algorithms I actually used while work... by Taek on Wednesday July 15, 2020 @03:55AM (#23842439)
We always tell our candidates in advance what algorithms we'll be quizzing them on. And it's pretty much always:

+ fibbonacci

+ a sort

+ a linked list

I like having candidates write out these problems on paper because it shows that they know how to think about code. Fibbonacci allows us to see that they have basic recursion understanding, and basic iterative loop understanding. Linked lists shows us that they understand pointers. And a sort shows us that you can structure more complex code well.

If you truly want the job, you'll take 15 minutes the night before to remind yourself how all of these things work, none of them should be particularly foreign or confusing to an experienced programmer.

That covers the coding part of our interview. For the problem solving part of the interview, we may give you problems that require using heaps or skiplists or graph algorithms, but for this part of the interview we're happy to let you import imaginary libraries that do all the hard work.

Re:Data structures and algorithms I actually used while work... by tchaffee on Wednesday July 15, 2020 @05:44AM (#23843060)
I already don't want the job because of the interview process. Talking to someone about code they have written and the decisions and thinking around their own code is so much more respectful and gives better signal. You should be doing everything you can to put the candidate on their own turf and letting them shine. I have a lot of advice about interviews but one of the best I've heard over the years: whatever impression you have of the candidate try to prove yourself wrong. Using that advice, I've found a lot of great programmers that other companies skip over. Some people who are great at coding are bad at interviews and bad at live problem solving with an audience. If you really feel the need for a test, a paid take home coding assignment will give you the best signal.
Re:Data structures and algorithms I actually used while work... by kypro on Wednesday July 15, 2020 @07:49AM (#23843870)
I agree. I really dislike being asked to produce code of any complexity in an interview setting, although I do think it's important to see a candidate's code to understand how they solve problems for more senior roles.

Personally I'm way too anxious in interview settings to produce decent code. In interviews I find myself trying to get everything right first time, but in reality that's not how I work. I prefer to develop iteratively and debug as I go.

I'm also quite a slow and deep thinker. It's not uncommon for me to think about a problem for a good 15-20 minutes before writing any code. In this case I guess you would have some time to prep the night before, but I'd still be very nervous about coding on the day.

I prefer home assignments and that's typically what I'll do when interviewing people. I like to keep it simple and open ended. Simple because I don't want to waste their time and open ended to allow them some room for creativity because often we don't have explicit requirements in the real world. This has always worked quite well.

Re:Data structures and algorithms I actually used while work... by monksy on Wednesday July 15, 2020 @03:12PM (#23850191)
Takehomes: The minute you allow for them to ask for 2-3 hours of work. That's the moment that they'll ask for 40+hours of work for free (and call it 2-3 hours).

Also they'll try to claim copyright on your work (with pay) and reject you outright.

Re:Data structures and algorithms I actually used while work... by tchaffee on Wednesday July 15, 2020 @03:21PM (#23850314)
I agree you have to be careful. However the takehomes I have had were a few hours of work paid at 50% of the hiring rate and that was where it ended. Great experience. There are companies out there doing this to make a great interview process and companies out there who want free work. It should become obvious which is which very quickly.
Re:Data structures and algorithms I actually used while work... by monksy on Wednesday July 15, 2020 @03:25PM (#23850364)
How could I be mad if you are guaranteeing pay for this moonlighting? At that time it's freelance work. However, if you're going to limit to 4 hours: If I don't have doc building copy, and tests done on that 4 hour mark, you're not getting those extras.
Re:Data structures and algorithms I actually used while work... by fatnoah on Wednesday July 15, 2020 @11:00AM (#23846119)
>I agree. I really dislike being asked to produce code of any complexity in an interview setting, although I do think it's important to see a candidate's code to understand how they solve problems for more senior roles.

I've never been a big fan of giving code interviews, however when I have to give them, I'm far more interested in the candidate's thought process around how they understand the problem and what needs to be considered in building the solution.

Re:Data structures and algorithms I actually used while work... by redblacktree on Wednesday July 15, 2020 @11:58AM (#23847098)
I agree, and I think this is where the value in these kinds of interview techniques lie. As the interviewee though, I have to say that the high-pressure situation makes me feel very... exposed(?) when I have to elucidate my thought process in real time.

I will say that one of the best "interviews" I've had included a portion where I worked with one of their engineers on a design problem they were actually having. It felt very collaborative, which allowed me to relax a bit, and I think we came up with a pretty good solution. I didn't mind doing "free work," because for me, it's fun to solve problems like these.

Re:Data structures and algorithms I actually used while work... by fatnoah on Thursday July 16, 2020 @11:30AM (#23859979)
>I agree. I really dislike being asked to produce code of any complexity in an interview setting, although I do think it's important to see a candidate's code to understand how they solve problems for more senior roles.

I've been around the block a few times and I LOVE system design interviews, both as an interviewer and interviewee. They are much more collaborative and "fun" for me. The good news is that I've been managing for several years at this point, so those types of interviews are far more prevalent than coding.

Re:Data structures and algorithms I actually used while work... by haggy on Wednesday July 15, 2020 @07:34AM (#23843769)
I agree with this 100%.

I'd also like to add that because of this I adapted the traditional whiteboarding exercise at my current company to be about problem solving and design, and not about how many data structures you've memorized.

When a candidate comes in, I give them a fake-yet-realistic product requirement (like count elements in a real-time stream from field sensors) and let them run with it however they see fit. I put zero restrictions on the tech side of things in order to let the candidate shine (hopefully) in whatever tech they are most comfortable with.

I make sure to ask for feedback on the exercise and I've gotten all positive feedback from candidates.

Re:Data structures and algorithms I actually used while work... by tchaffee on Wednesday July 15, 2020 @07:49AM (#23843869)
It sounds like you're trying which is encouraging. I'll give you a few tips that I hope help.

> I adapted the traditional whiteboarding exercise

Lots of engineer types freeze when they have to make a presentation. I remember a meeting early in my career with literally three people in a conference room and I almost had a panic attack. No white board. People I already knew. All I had to to do is explain my ideas to three people. I did improve. I've given many public talks sometimes to hundreds of people and today I like public speaking. But I'll never forget where I started and that the person I'm interviewing might be very nervous. Putting them up on a whiteboard and into presentation mode only amplifies that nervousness. Just talk to people. Get them into that flow that comes from talking about something familiar that they are excited about. For most people a whiteboard test is not exciting. But if you give me a paid take home coding exercise, that's exciting. I like code, and I like to get paid for it. So if you really think you need a test, consider a very high signal test that is the exact work they will be doing.

> I make sure to ask for feedback on the exercise and I've gotten all positive feedback from candidates.

In an unequal relationship like that you are going to get a lot of false positives. If I really needed a job my response would be "loved the exercise, looking forward to talking to you more and discussing next steps if I'm the right candidate".

Re:Data structures and algorithms I actually used while work... by GeneralMayhem on Wednesday July 15, 2020 @08:32AM (#23844235)
>Lots of engineer types freeze when they have to make a presentation. I remember a meeting early in my career with literally three people in a conference room and I almost had a panic attack.

That's definitely an unfortunate experience, but isn't giving presentations to explain your ideas - sometimes to people you don't really know - actually a significant part of the job? I'm an IC, and I present designs and project plans for feedback to groups of up to a couple dozen people, or to VPs, on a pretty regular basis. If I couldn't do that competently I would be failing at my job, because that kind of communication is just as important as actually writing the code. If I were conducting an interview and discovered that "public" speaking on the order of a few people in a conference room was extremely difficult for you, I'd probably consider that an important red flag.

Re:Data structures and algorithms I actually used while work... by tchaffee on Wednesday July 15, 2020 @08:46AM (#23844379)
What is an IC?

> isn't giving presentations to explain your ideas - sometimes to people you don't really know - actually a significant part of the job?

For many engineering positions it is not. There are loads of shops where the developers, even senior developers, mostly just write code. I have hired many of them and put them to work successfully building stuff while I deal with the meetings.

> If I were conducting an interview and discovered that "public" speaking on the order of a few people in a conference room was extremely difficult for you

And yet I worked very successfully as a junior developer for a couple of years before I ever had to attend a meeting like that. Among the other 100 devs in the shop, I was considered a top talent at coding. That's not to brag but to point out that I was adding lots of value to the company without having to attend meetings with people from other departments. My manager did that. My manager did not code, although he could if he wanted to. In fact at that company they created a separate path for highly talented engineers who wanted to continue coding but wanted to avoid management and meetings. That was a couple of decades ago and less common, but I think it's a lot more common now.

And the fact that I can now publicly speak in front of hundreds of people should hopefully encourage you that it's something people can learn if they want to. Some people don't want to. There are roles for them too.

Re:Data structures and algorithms I actually used while work... by GeneralMayhem on Thursday July 16, 2020 @08:06PM (#23865385)
>What is an IC?

Individual Contributor, used to contrast with "Manager". Point being, my main job is to design systems and write code, but I still have to do some of the organizational stuff sometimes.

>There are loads of shops where the developers, even senior developers, mostly just write code.

Okay, so your experience is different from mine. I'm surprised that there are places where this is really not a meaningful job requirement, and I've never worked at one, but I believe you.

>it's something people can learn if they want to

Sure... but so is coding. Part of the point of an interview is to see what skills and traits you already have. If you're missing some, that can be balanced against the ones you do have - it's not a dealbreaker, but it is still a negative.

>Some people don't want to. There are roles for them too.

Again, not where I work. Engineering, even at relatively junior levels, includes collaboration and explaining your work. You can't just say you don't want to do that - or rather, you can, but you'll have no upward mobility and be managed out pretty quickly.

Re:Data structures and algorithms I actually used while work... by unclewalter on Wednesday July 15, 2020 @09:04AM (#23844582)
> What is an IC?

Believe op is using it as "individual contributor"

Re:Data structures and algorithms I actually used while work... by arethuza on Wednesday July 15, 2020 @11:24AM (#23846555)
Isn't there just a bit of condescension in that title? It always sounds to me like something invented by a manager to be dismissive of someone who doesn't manage anyone.
Re:Data structures and algorithms I actually used while work... by mindcrime on Wednesday July 15, 2020 @01:41PM (#23848722)
I never got the vibe that "IC" was intended to be dismissive, or intended to be a pejorative or anything. I think it's more of an HR thing, because the distinction between IC vs Manager usually affects compensation and blah, etc. Whether or not that should be the case is a separate question.

I always assumed the term just leaked out of the HR world and into the general parlance.

Re:Data structures and algorithms I actually used while work... by dmoy on Wednesday July 15, 2020 @03:07PM (#23850119)
It depends on the culture setting.

If you're in a place where an IC can potentially make half a million or million dollars a year and drive some very interesting project work and be highly respected, no.

If you're in a place where not making it into manager track puts a very low ceiling on earning potential and respect, then yes it can be condescending.

This varies a lot by country, industry, etc.

Re:Data structures and algorithms I actually used while work... by VRay on Wednesday July 15, 2020 @12:13PM (#23847337)
I think you're projecting

I've never heard of someone not liking the term "Individual Contributor" before

Re:Data structures and algorithms I actually used while work... by yahyaheee on Wednesday July 15, 2020 @09:09AM (#23844645)
That’s not the same as on the spot coding challenges in a whiteboard though
Re:Data structures and algorithms I actually used while work... by TedDoesntTalk on Wednesday July 15, 2020 @09:29AM (#23844914)
You’re right. But what are you trying to prove with on the spot coding challenges? That the candidate can implement a hash table from scratch in 15 minutes with O(1) performance?

Is that something they’re going to do on the job? Very unlikely. So why are you not only testing for it but testing for it in a fake time-pressured stressful situation that also rarely exists on the job? (Yes , we all have deadlines but when are they ever 15 minutes on the spot?)

Perhaps you should have entered the education field where you could give such exams day after day to your heart’s content?

Re:Data structures and algorithms I actually used while work... by giantrobot on Wednesday July 15, 2020 @12:27PM (#23847546)
People giving on the spot coding exams, or worse whiteboard coding exams are just assholes. They're trying to haze interviewees and make themselves feel better. There's zero actual utility to such tasks in an interview.
Re:Data structures and algorithms I actually used while work... by LandR on Wednesday July 15, 2020 @10:43AM (#23845849)
> Lots of engineer types freeze when they have to make a presentation.

I've seen one place that wanted developers to do a full presentation with slides and all to a group of people.

The topic of the presentation was completely up to you, didn't even have to be a technical subject. You could do a presentation on baking if you wanted.

Was doing presentations a part of the job of the developer? No. They wouldn't have to do presentations in their day job...

I didn't go for the job and have never understood what they were looking for.

Re:Data structures and algorithms I actually used while work... by TedDoesntTalk on Wednesday July 15, 2020 @09:21AM (#23844800)
I do something similar: I ask the candidate to take 30 minutes or so to design a system like an online library. What are the objects in the system, how do they relate, can you design a REST or graphQL API for CRUD operations.

Then if it’s a front-end position we can move onto UI components for it; for back-end or full stack I focus on implement a couple of the CRUD operations.

You really get to see how people think and will work on the job with questions like this.

Implementing a sorting algorithm from scratch is a waste of time in many projects: use your libraries and get on with life. That’s why such interview questions are not important to me.

Re:Data structures and algorithms I actually used while work... by nogabebop23 on Wednesday July 15, 2020 @11:52AM (#23847004)
>> You really get to see how people think and will work on the job with questions like this.

Interviewing is hard and I applaud you for trying something newer and more realistic but I'd be cautious in thinking "spend 30 minutes designing a system and be prepared to defend your work with massive risk/reward hanging in the balance. Begin... NOW!!!!" is representative of how someone will work on the job. There are an awful lot of people who will solve the crux problems driving home after the interview or the next morning in the shower vs. on-demand.

Re:Data structures and algorithms I actually used while work... by TedDoesntTalk on Wednesday July 15, 2020 @04:53PM (#23851616)
Very true.

I certainly don’t say BEGIN NOW! It’s a conversation. I continually ask questions during the design and ask the candidate to speak about the decisions being made.

In all honesty this process can easily last more than 30 minutes.

This is not a hard problem for someone with the experience I want, and there are many different correct answers.

Re:Data structures and algorithms I actually used while work... by abower on Wednesday July 15, 2020 @08:22PM (#23854215)
Agree. Its far more valuable to see how a candidate approaches problems than some memorized recital show. I don't expect perfection, but if their reasoning is sound that becomes clear fairly quickly. I usually also include some imperfect questions purposely to see how they handle that. While we wish client projects were nice and clean with orderly data and tight requirements, the truth is that is rarely the case. It's useful to see how the candidate responds to those types of things. I had one gentleman argue with me about one of those questions claiming you would never see primary and foreign keys between tables not perectly match in the real world. Well when you are matching data from entirely disparate systems, you typically do. Anyway I digress.
Re:Data structures and algorithms I actually used while work... by heavyset_go on Wednesday July 15, 2020 @07:53AM (#23843899)
> Talking to someone about code they have written and the decisions and thinking around their own code is so much more respectful and gives better signal.

Agreed. The best jobs I've taken had interviews like this, or paid assignments.

Re:Data structures and algorithms I actually used while work... by Taek on Wednesday July 15, 2020 @09:49AM (#23845165)
Talking about code that someone has already written is missing the element of productivity. Since we pay you by the week and only get so many hours a week out of you, we need some way before we hire you to understand what type of output we can expect.

The best way I know would be to work with someone for a week or two on a real problem, but that's way too expensive to trial a junior role.

At least for us, the system is more optimized around avoiding false positives than ensuring we don't pass over someone good.

Re:Data structures and algorithms I actually used while work... by sushshshsh on Wednesday July 15, 2020 @09:51AM (#23845189)
Your current system is better. I'm happy to take 15 minutes of my life to prepare for your interview instead of 1 week.

Since the denial rate in our industry is so high, you can see which one is more preferable.

Re:Data structures and algorithms I actually used while work... by Yajirobe on Wednesday July 15, 2020 @11:23AM (#23846516)
Programming in an interview environment is completely different from programming at your 9-5 desk.
Re:Data structures and algorithms I actually used while work... by recursive on Wednesday July 15, 2020 @11:45AM (#23846886)
Everything else in an interview is also completely different from 9-5 engineering.
Re:Data structures and algorithms I actually used while work... by tchaffee on Wednesday July 15, 2020 @04:57PM (#23851689)
Due to a lack of imagination and people who don't host interviews on a regular basis so they never put in the effort to get good at it. When I conduct a technical interview we are going to end up doing some stuff you do in your 9-5. And you probably won't even notice it. And if there is a budget available for it, I'm going to pay you to do a very small coding assignment. A few hours. Almost exactly your 9-5.
Re:Data structures and algorithms I actually used while work... by recursive on Wednesday July 15, 2020 @06:21PM (#23852975)
The fundamental difference is that a job is directly at stake based on your performance. For many people, this makes them more nervous than they would ever generally be during work. Due to the nature of interviews, I don't think there's really a way for it to be otherwise.
Re:Data structures and algorithms I actually used while work... by nogabebop23 on Wednesday July 15, 2020 @11:48AM (#23846944)
>> The best way I know would be to work with someone for a week or two on a real problem,

So if I get the job will we just touch base ever week or so? This sounds unlikely. Why can't you break work down to the point that you can pay me for 3-4 hours of real output and then use that to evaluate my quality?

All these tests around recursion and linked lists, etc are a feel-good proxy for actually measuring suitability, If someone is actually implementing them (IF!) it's not the junior, new-person; it's the senior architect who does it once every few years in a shared library. I can count the number of times I've used recursion in (a) a non-toy implementation and (b) not ripped it out and replaced with a faster, clearer iterative implementation on one hand.

Re:Data structures and algorithms I actually used while work... by shecky on Wednesday July 15, 2020 @12:42PM (#23847779)
> Talking to someone about code they have written and the decisions and thinking around their own code is so much more respectful and gives better signal

where have you been all my life? ;)

I'm an autodidact, zero formal bg in comp sci. I suck at timed tests and algo interviews. Fifteen years I've been at this and I've worked with several 'full stack' teams, none of which had a single engineer who was within a thousand miles of what I could do with CSS (and they're mostly utter slobs wrt HTML). And as to the endless javascript demands, I will never be of interest to google (and the feeling's mutual) but I always get the job done, and often the job is something FE that none of my esteemed colleagues would know the first thing about how to achieve, comp sci degrees and recursion expertise notwithstanding ... not to mention that every one of them has as many stack overflow tabs open as me.

I'll get back to my sorry little js projects now, maybe I'll get another job before I grow old and die ;)

Re:Data structures and algorithms I actually used while work... by ajzinsbwbs on Thursday July 16, 2020 @12:34AM (#23855797)
How to filter out candidates who are only good at talking? I’ve worked with people like that and it’s miserable.
Re:Data structures and algorithms I actually used while work... by sufyanadam on Wednesday July 15, 2020 @10:05AM (#23845376)
You nailed it. This is the fairest proposal with the best outcome for all sides. Thank you for posting.
Re:Data structures and algorithms I actually used while work... by sushshshsh on Wednesday July 15, 2020 @09:49AM (#23845171)
Good thing you don't want it because I'll happily take it.
Re:Data structures and algorithms I actually used while work... by lootsauce on Wednesday July 15, 2020 @05:11AM (#23842866)
This sounds like a reasonable approach and that is why software interviews remain broken. I'm sure it works for your organization, not saying you are bad at hiring or anything but it still smacks of the kind of hoop-jumping that turned me off so much from the process last time I was interviewing. This included on-the-spot coding exercises, massive take-home projects that required many hours of undifferentiated grunt work, totally useless whiteboard sketching and pseudo-code sessions, obscure Google-like quiz questions.

The interview process for the job I have now was a massive breath of fresh air.

The application asked for code samples and a cv. It was a small company and the CEO, CTO and direct co-workers all drove the interview process. The process was entirely conversational. First an intro phone call with the CTO and then a questionnaire via email in which I answered about 30 questions on various topics that were all very practical daily software development type stuff. It was painless to respond to each with about a paragraph in detail. Then a call with a direct co-worker about the questionnaire and this was my opportunity to ask questions of him about the company. Before getting the interview they actually read my code samples and reviewed my Github account. In the interview there was a ton of discussion about the company, its culture and all the of the above discussions. Following this was compensation negotiation with the CEO.

Everything about the hiring process said to me yes this is the place, they get it!

Re:Data structures and algorithms I actually used while work... by psds2 on Wednesday July 15, 2020 @07:16AM (#23843648)
Many people would consider a 30 question take home project massive. You discuss your second experience as if it is some novel Utopian experience but when I read it sounds like an experience that was well suited to your strengths but wouldn't be suited to mine.
Re:Data structures and algorithms I actually used while work... by lootsauce on Wednesday July 15, 2020 @08:14AM (#23844077)
It definitely favored verbal skills, I am assuming that was a feature not a bug in the hiring process. The questions were not a project but a conversation in the context of my experience With a specific tech stack such as... “How do you keep a server from getting hacked”, “how do you handle conflicting dependencies”, “how do you know your code is ready to deploy to production”
Re:Data structures and algorithms I actually used while work... by watwut on Wednesday July 15, 2020 @09:18AM (#23844750)
Isnt writing paragraph of response way more work then checking out linked list and going on interview?
Re:Data structures and algorithms I actually used while work... by arethuza on Wednesday July 15, 2020 @04:32AM (#23842647)
"write out these problems on paper"

I write on paper so infrequently that I actually find it pretty difficult to write more than a few words. I certainly wouldn't want to write something out longhand in an interview!

Edit: It seems to me it would be rather unfair of me to ask people to write out their thoughts in Org Mode in VS Code just because that's how I happen to like writing notes :-)

Re:Data structures and algorithms I actually used while work... by robotron on Wednesday July 15, 2020 @09:26AM (#23844874)
I had to do a timed exercise to write code on paper IN PEN at a job interview. Talk about feeling like I had to get it correct the first time. Glad they didn't give me an offer because I would've had to consider accepting it.
Re:Data structures and algorithms I actually used while work... by arethuza on Wednesday July 15, 2020 @11:25AM (#23846583)
You problem used tabs rather than spaces ;-)

Seriously though - how long ago was that - not recently I hope?

Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @05:22AM (#23842918)
Writing speed isn't the bottleneck hopefully :)
Re:Data structures and algorithms I actually used while work... by fenomas on Wednesday July 15, 2020 @05:45AM (#23843069)
It's not a matter of writing speed, it's familiarity. I've been programming for 30 years and I don't think I've ever written code down on paper. Why ask an interview candidate to do something they've never done before and will never do again? You might as well ask them to type their code using only one hand.
Re:Data structures and algorithms I actually used while work... by TedDoesntTalk on Wednesday July 15, 2020 @09:36AM (#23844989)
> I've been programming for 30 years and I don't think I've ever written code down on paper

I did it once or twice when I was 8 years old and didn’t have a computer yet :)

Re:Data structures and algorithms I actually used while work... by toomanybeersies on Wednesday July 15, 2020 @07:50AM (#23843881)
As a heavy smoker, I'd be quite good in an interview where I was forced to type with one hand.
Re:Data structures and algorithms I actually used while work... by arethuza on Wednesday July 15, 2020 @05:48AM (#23843083)
Writing speed probably would be the bottleneck for me - plus whatever I wrote would almost certainly be illegible to anyone else.
Re:Data structures and algorithms I actually used while work... by onion2k on Wednesday July 15, 2020 @10:05AM (#23845372)
I like having candidates write out these problems on paper because...

...because you like working through problems on paper, and your interview process is designed to find people like you.

Re:Data structures and algorithms I actually used while work... by pps43 on Wednesday July 15, 2020 @08:54AM (#23844465)
> Fibbonacci allows us to see that they have basic recursion understanding

I see no reason to use recursion when asked to calculate Fibonacci numbers. A loop looks like a more reasonable choice that also avoids typical pitfalls associated with recursion. Maybe that's because I did embedded programming for a while.

I suspect recursion is introduced in CS classes with this example, but people understand it as "you are supposed to use recursion to calculate Fibonacci numbers".

Re:Data structures and algorithms I actually used while work... by paulryanrogers on Wednesday July 15, 2020 @09:07AM (#23844623)
Recursion is often less efficient but looks more elegant and simpler. It breaks the problem down to is essence. Then you can trade some complexity for more run-time efficiency.
Re:Data structures and algorithms I actually used while work... by pps43 on Wednesday July 15, 2020 @09:17AM (#23844739)
> more elegant and simpler

Beauty is in the eye of the beholder, but a loop is hard to beat as far as simplicity goes, and you don't depend on your compiler being clever enough to optimize tail recursion.

If you need to traverse a tree then sure, but with Fibonacci you don't even need the stack to begin with. You only need to keep a previous number.

Re:Data structures and algorithms I actually used while work... by duskwuff on Wednesday July 15, 2020 @07:06PM (#23853491)
You don't even need a loop. Binet's formula gives a closed-form expression for the Fibonacci sequence. (And of course I don't remember that formula off the top of my head, but I know it exists, so I'd be 90% of the way to solving a problem which required it.)
Re:Data structures and algorithms I actually used while work... by pps43 on Wednesday July 15, 2020 @09:37PM (#23854775)
This will open another can of worms (floating point errors).
Re:Data structures and algorithms I actually used while work... by ricksharp on Wednesday July 15, 2020 @09:23AM (#23844823)
This sounds like an inspection of whether the person has had a class in basic algorithms rather than if they have ever coded anything in real life.

In school I played with sorting algorithms, in business if I ever found a developer manually writing a sorting algorithm, I would consider them inept (unless there were very specific reasons to do so).

If someone didn’t know how to sort a list using the built in or standard library of whatever language they are using, I would know very quickly that they have almost no experience writing anything useful.

Re:Data structures and algorithms I actually used while work... by tsimionescu on Wednesday July 15, 2020 @09:53AM (#23845220)
If you're given a blank slate and asked to sort a list without using a library without any gotchas, complexity requirements, space requirements, expectations that the code is completely free of small bugs etc.; and you can't do that after some thinking even to a basic degree, then you're just not a good programmer.

As a programmer, your task is to find algorithms to solve problems. Sure, sorting numbers is a solved problem and in real life you should use a library. But you will not find a library for each problem you encounter, and sorting is a very simple problem to solve compared to anything you'll find in real life.

Re:Data structures and algorithms I actually used while work... by ivanhoe on Wednesday July 15, 2020 @10:23AM (#23845585)
I agree, but how is applying pre-learned algorithms significantly better than using pre-build libraries? Both is about using an existing solution, instead of coming up with it on your own. The real approach would be to ask candidates to solve a problem that doesn't easily fit into any popular category, so that they have to come up with an original approach - but that is first hard to come up with, and 2nd it would require investing extra time on both sides, which usually neither candidates nor interviewer can afford.
Re:Data structures and algorithms I actually used while work... by tsimionescu on Thursday July 16, 2020 @02:04AM (#23856257)
Yes, that's a good point, and in our interviews we do try to take a slightly less well-known problem, at least one that doesn't have a dozen well-known named algorithms.

Still, even using a pre-learned algorithm demonstrates some level of confort with algorithms in general, and can be used as a stepping stone during the interview to shift the problem slightly to see how they adapt the algorithm.

For example, if a candidate is clearly taking their time and essentially working out the algorithm themselves, and succeeding, that demonstrates the skill and we can move to other problems. If the candidate just breezes through and finishes in 2 minutes, then we can change the problem ad-hoc to try to get them off the beaten track a bit (say, if they wrote quicksort, maybe ask about making it stable, or sorting even numbers differently from odd numbers, off the top of my head).

Re:Data structures and algorithms I actually used while work... by ricksharp on Wednesday July 15, 2020 @11:43PM (#23855527)
My point was that if testing for a skillset, using something that favors those who have toyed with a specific algorithm is the same as asking trivia questions.

Trivia does not provide evidence of skill, it demonstrates prior knowledge.

It would be better to introduce a unique situation that would place everyone at the same starting point.

Re:Data structures and algorithms I actually used while work... by tsimionescu on Thursday July 16, 2020 @02:10AM (#23856287)
If the expectation of the interviewer is to see if they know quicksort, then I fully agree with you. If the sorting problem is simply used as a (rather uninspired, granted) stepping stone to checking that the candidate has basic algorithm-finding skills, then I think it can serve this purpose (though less well-known but still relatively simple problems would be better). I gave an example in another thread as well, but in principle I would change the problem up a bit if the candidate is obviously producing a well-known algorithm; whereas, if the candidate is working out the solution themselves, I would be happy with that and move to other topics.

Even so, knowing very widely talked about programming trivia is still a signal for interest in the art. I'm not sure that sorting algorithms are a good example of what I'm thinking, but I always award extra mental points to candidates who seem knowledgeable about the field (e.g. they know the general consensus on manual memory management vs garbage collection). Still, I wouldn't consider these sorts of things dealbreakers by any stretch of the imagination.

Re:Data structures and algorithms I actually used while work... by ricksharp on Wednesday July 15, 2020 @09:48AM (#23845154)
In fact now that I think about it, it might be a good test to see if they are passionate enough to be a great problem solver:

When was the last time you wrote a sorting algorithm?

Acceptable answers:

- You mean sorting a list? - In school - Why would I write a sorting algorithm? - Never - (Glazing eyes and other body language that indicates frustration)

Red Flags:

- Anything that indicates they think writing a sorting algorithm is a perfectly logical thing to do during the course of software development.

(Note: The problem is sorting may seem like a well-understood problem, but it’s actually not. Only a person who studied the algorithms in school as an assignment would feel comfortable with that code.)

Re:Data structures and algorithms I actually used while work... by craftinator on Wednesday July 15, 2020 @09:54AM (#23845246)
Acceptable answer: it depends on what I'm sorting. Is it a primitive like char strings, numbers, or a more complex data type? Could it have null values and type mismatches, am I using a strongly typed language? Does it need to be cleaned first?

I think the ability to come up with relevant and important questions to ask about the task given is more crucial than whether they can solve it. You can look up answers to questions from hundreds of good sources, but you need to be able to generate the right questions that need to be answered first.

Re:Data structures and algorithms I actually used while work... by maaaaattttt on Wednesday July 15, 2020 @10:05AM (#23845375)
> you need to be able to generate the right questions

Completely agree with this statement.

I would add that having the proper vocabulary is an important part of generating the right questions.

Re:Data structures and algorithms I actually used while work... by the_mitsuhiko on Wednesday July 15, 2020 @04:44AM (#23842720)
> I like having candidates write out these problems on paper because it shows that they know how to think about code.

Is this really a useful exercise? Why not present them with an actual problem that is relevant to your field and see how they approach it?

Re:Data structures and algorithms I actually used while work... by entha_saava on Wednesday July 15, 2020 @04:55AM (#23842781)
To filter out naive people that don't have proper understanding of fundamentals and just having some framework plumbing knowledge, I guess.

These are considered pretty basic stuff that every programmer should know..

Re:Data structures and algorithms I actually used while work... by p1necone on Wednesday July 15, 2020 @05:23AM (#23842922)
In my experience it's pretty necessary to do this. Probably depends on your local job market, but there are a shocking number of candidates that just don't know how to code.

The explanation I've heard is that good devs generally get hired after only a handful of interviews, whereas really bad devs are going to do a lot more interviews on average before they get hired, so you get a pretty skewed sampling even if there aren't that many really bad candidates around.

Re:Data structures and algorithms I actually used while work... by entha_saava on Wednesday July 15, 2020 @05:45AM (#23843066)
Yes, Joel Spolsky and IIRC Jeff Atwood have written somewhat extensively about it.

We are in a bubble, if we read programming blogs and think about programming in our free time, we are definitely not the kind that FizzBuzz exists to filter out. But from the perspective of companies, it makes sense if they really understood pointers or recursion or graph manipulation, because there are so many people lying on their resumes, not having sufficient analytical skills despite doing some resume driven cargo cult development etc.. And as an industry we don't really have an alternative to these algorithm interviews at scale, at the point we rely on non technical HR people to filter out resumes for us and they literally grep for framework/language experience.

Re:Data structures and algorithms I actually used while work... by dogfoods on Wednesday July 15, 2020 @11:52PM (#23855587)
Real jobs have certificates and degrees that mean something, where you can avoid the whole "do you know literal 101 things" phases of the interview process by just going "do you see the line on my resume that says M.Sc."
Re:Data structures and algorithms I actually used while work... by the_mitsuhiko on Wednesday July 15, 2020 @06:12AM (#23843239)
I don't know what your hiring experience with this is, but there is an entire market around "coding interviews" where people will learn how to pass these. I found algorithmic interviews completely useless to assess junior engineers because of how many just learn just to pass interviews, but then have very little experience with real problems.
Re:Data structures and algorithms I actually used while work... by heavyset_go on Wednesday July 15, 2020 @08:02AM (#23843973)
If you want an example of this, check out r/cscareerquestions. The standard advice is not to practice any practical coding with projects, but to "grind leetcode" to get past interview filters.
Re:Data structures and algorithms I actually used while work... by watwut on Wednesday July 15, 2020 @09:19AM (#23844773)
If you want experience with real problems, they juniors should not be your thing. Juniors are supposed to be people with little experience that are able to work if given tasks by seniors.
Re:Data structures and algorithms I actually used while work... by pkaye on Wednesday July 15, 2020 @02:36PM (#23849635)
A lot of candidates can't reason past the simple stuff so rarely do I have go beyond a simple coding problem. The FAANGs and unicorns maybe get lots of candidates so they can be very choosy.
Re:Data structures and algorithms I actually used while work... by no_wizard on Wednesday July 15, 2020 @10:00AM (#23845297)
I’ve been interviewing at a lot of places lately. I _never_ white board interviewed before this round of interviews I’ve done And at first I have to say that I hated it so much.

In a weird way though, I’ve at least grown to understand why it’s so popular, and I don’t think it’s just because FAANGS do it (though I think that’s how it came to prominence). It boils down to showing as an engineer your capable of being flexible and malleable your change and understanding. Some places definitely do this better than others, and I think it says a lot about company culture if they are Adversarial about it or not. In my experience rather unfortunately even good places to work can be adversarial about it.

The massive downside is and always will be it puts so much onerous on the candidate because you don’t really know what they are going to ask and freezing up or getting stumped is very frowned upon in these style interviews from what I experienced.

My tip to anyone though is just to practice, but I’ve yet to encounter places that don’t employ some technique to test algorithmic thinking and understanding of complex systems in some way.

Re:Data structures and algorithms I actually used while work... by fanf2 on Wednesday July 15, 2020 @04:34AM (#23842660)
Why would anyone use recursion to calculate Fibonacci numbers other than as a microbenchmark for function call performance?
Re:Data structures and algorithms I actually used while work... by bbarn on Wednesday July 15, 2020 @04:58AM (#23842797)
It's a great showcase of how a flashy looking solution is the wrong approach. A good candidate will know it can be written in 2 lines recursively, but that the stack will explode with a fairly low term number, and that iterating with a for loop is more efficient.
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @05:02AM (#23842816)
In eg Python you can just add a memoization decoration, and get a linear solution from the naive recursive one.

That's pretty neat.

Re:Data structures and algorithms I actually used while work... by dotancohen on Wednesday July 15, 2020 @05:51AM (#23843106)
In almost any language, a hashtable check after the base case check could be used in a similar manner.
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @06:34AM (#23843370)
Yes. I just singled out Python, because the language is well-known, and adding the memoization is particularly straight-forward.
Re:Data structures and algorithms I actually used while work... by ForHackernews on Wednesday July 15, 2020 @05:53AM (#23843124)
Fibonacci has a closed-form solution! Forget writing loops, you can write one damn equation. Runs in constant time.
Re:Data structures and algorithms I actually used while work... by WJW on Wednesday July 15, 2020 @07:36AM (#23843781)
The closed form solution is technically O(phi^N), so still exponential. It comes mostly from exponentiation not being constant time, see https://stackoverflow.com/questions/360748/computational-com.... It'll only be constant time if your values fit into a hardware register and you can leverage the exponentiation instructions of your CPU.

There is a O(log(N)) solution involving matrix exponentiation though, if you really need to get the big numbers.

Re:Data structures and algorithms I actually used while work... by Taek on Wednesday July 15, 2020 @10:09AM (#23845428)
Technically, Fib(n) at unbounded sizes is at least linear to compute. This is because the output of Fib(n) has O(n) bits in it. So even if the computation is free, it's still O(n) just to print the result.

That's not the type of thing I would ever expect a candidate to know in an interview, just something fun I've run across.

Re:Data structures and algorithms I actually used while work... by Thrymr on Wednesday July 15, 2020 @11:36AM (#23846749)
Or in this case, just compute it from the closed form expression with no loop at all.
Re:Data structures and algorithms I actually used while work... by dotancohen on Wednesday July 15, 2020 @05:45AM (#23843063)
> iterating with a for loop

I'm struggling to understand what you mean? This is a learning opportunity for me, if you wouldn't mind posting a code sample or elaborating further.

Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @06:03AM (#23843172)
Something like this (might be subtly wrong, I wrote it in 2 minutes).

    int fib(int n) {
       if (n<=2)
          return 1;
       int fibNMinus2= 1;
       int fibNMinus1 = 1;
       int tmp;
       for (int i=3; i<=n; i++) {
           tmp = fibNMinus2+ fibNMinus1 ;
           fibNMinus2 = fibNMinus1;
           fibNMinus1 = tmp;
       }
       return fibNMinus1;
    }
vs recursive solution which is pretty but slow (and will fail when you run out of stack)

    int fib(int n) {
       if (i<=2)
          return 1;
       return fib(n-1)+fib(n-2);
    }
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @06:49AM (#23843473)
Of course, recursion vs iteration is mostly an implementation detail. Here's a recursive version (expressed in Python) that works better than your loop:

    def fib(n):
      if n =< 0:
        return (0, 1)
      else:
        a, b = f(n-1)
        return (b, a + b)
(I say it works better, because it has the same asymptotic runtime, but fails better: When numbers get large, your C version will run into undefined behaviour that can cause arbitrary problems. The Python version will just crash with a well-defined exception.

A better language than Python can run this recursive version for arbitrarily big numbers.)

Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @07:18AM (#23843658)
In practice iteration will be faster, even in Python (because there's less overhead).

But yes, I should have written "naive recursive solution". There are many ways to fix it. It just wasn't what the question was about.

Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @08:02AM (#23843971)
In Python, yes.

If you have a decent language and a good compiler / interpreter, then the recursion with function calls will have no overhead over iteration. (Basically, in Haskell or Scheme your recursion will be compiled into the same machine language sequence of straight-line code plus conditional jump as the iterative loop.)

But, agreed with everything else you wrote!

Re:Data structures and algorithms I actually used while work... by tom_mellior on Wednesday July 15, 2020 @09:55AM (#23845251)
> If you have a decent language and a good compiler / interpreter, then the recursion with function calls will have no overhead over iteration.

The recursive Python version above didn't use tail calls. I don't think that a Haskell or Scheme compiler would compile the equivalent versions into a simple loop.

Re:Data structures and algorithms I actually used while work... by eru on Saturday July 18, 2020 @04:25AM (#23879961)
I just tried it out, I didn't manage to get GHC to compile the equivalent of the code I've given into something that runs in constant memory. (I did the calculations modulo some number, to keep the numbers themselves bounded.)

GHC is sometimes able to do some of those transformations.

> The recursive Python version above didn't use tail calls.

Just to be more pedantic: it did use tail calls, but the recursive calls weren't the tail calls.

Re:Data structures and algorithms I actually used while work... by tom_mellior on Sunday July 19, 2020 @02:05PM (#23891128)
> Just to be more pedantic: it did use tail calls, but the recursive calls weren't the tail calls.

I was talking about this code:

    def fib(n):
      if n =< 0:
        return (0, 1)
      else:
        a, b = f(n-1)
        return (b, a + b)
Not sure what you mean by tail calls here. I don't see Python-level tail calls. The interpreter will call C code for tuple packing, but those aren't in tail position with respect to the Python code either.
Re:Data structures and algorithms I actually used while work... by eru on Tuesday July 21, 2020 @01:50AM (#23904598)
The last call (to a function or operator) is in the tail position. In this case, it is tuple packing.

Why wouldn't the tuple packing be in tail position in the Python code?

Re:Data structures and algorithms I actually used while work... by tom_mellior on Wednesday July 22, 2020 @03:26AM (#23914750)
Because after tuple packing returns (a C return) you still need to perform a Python return. The C tuple packing function cannot return directly from the Python function.

Looking at https://github.com/python/cpython/blob/master/Python/ceval.c, here is the code that needs to be executed after the tuple is constructed:

        case TARGET(RETURN_VALUE): {
            retval = POP();
            assert(f->f_iblock == 0);
            assert(EMPTY());
            f->f_state = FRAME_RETURNED;
            f->f_stackdepth = 0;
            goto exiting;
        }

    // ...

    exiting:
    if (tstate->use_tracing) {
        if (tstate->c_tracefunc) {
            if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
                                     tstate, f, PyTrace_RETURN, retval)) {
                Py_CLEAR(retval);
            }
        }
        if (tstate->c_profilefunc) {
            if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
                                     tstate, f, PyTrace_RETURN, retval)) {
                Py_CLEAR(retval);
            }
        }
    }

    /* pop frame */
    exit_eval_frame:
    if (PyDTrace_FUNCTION_RETURN_ENABLED())
        dtrace_function_return(f);
    _Py_LeaveRecursiveCall(tstate);
    tstate->frame = f->f_back;

    return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
I guess you could duplicate all this code in a special "pack tuple and return from Python function" C function which you could then really tail call.
Re:Data structures and algorithms I actually used while work... by eru on Friday July 24, 2020 @08:22AM (#23938605)
Oh, yes, I was only saying that as far as the Python language is concerned the tuple packing is in the tail position.

Any specific implementation, and in this case cpython, could do arbitrary weird things after.

Thanks for looking up the code!

Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @08:14AM (#23844078)
I can't be sure without measuring, but I strongly suspect the overhead will be mostly in tuple packing/unpacking and GC.

And I instinctively distrust Sufficiently Smart Compilers ;)

Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @09:38AM (#23845025)
Oh, your compiler doesn't have to be sufficiently smart. It merely has to avoid premature optimization: see this classic paper https://dspace.mit.edu/handle/1721.1/5753 by Guy Steele.

> I can't be sure without measuring, but I strongly suspect the overhead will be mostly in tuple packing/unpacking and GC.

I guess that's the same overhead as in this imperative version:

    a, b = 0, 1
    for _ in range(n):
      a, b = b, a + b
Re:Data structures and algorithms I actually used while work... by crimsonaluca20 on Wednesday July 15, 2020 @10:18AM (#23845531)
>(Basically, in Haskell or Scheme your recursion will be compiled into the same machine language sequence of straight-line code plus conditional jump as the iterative loop.)

This is not true. The recursive code will not compile with zero overhead even in haskell or lisp.

  fib :: int -> int
  fib 0 = 1
  fib 1 = 1
  fib n = fib (n - 1) + fib (n - 2)
The code above will add layers to the call stack and has a speed complexity of O(N) and memory complexity of O(N).

To optimize in Haskell you need to deliberately restructure your code.

  fib :: int -> int
  fib n = let _fib 0 a b = a
              _fib 1 a b = b
              _fib n a b = fib (n - 1) b (a + b) 
          in _fib n 0 1
The above code will have O(N) speed complexity and constant memory in Haskell. In order for Haskell or any language with tail recursion optimization to work the recursive call must take up the entire return expression. In short the coder must deliberately make optimizations and write recursion in a way similar to the original iterative syntax in order for such tricks to work.
Re:Data structures and algorithms I actually used while work... by eru on Saturday July 18, 2020 @04:30AM (#23879985)
Yes.

Though GHC is sometimes able to do some simple transformations on its own. But not in this case.

For anyone trying this at home: I suggest calculating your additions modulo some constant, so that the numbers involved stay within a fixed size. (Otherwise, you either hit the limits of Int and weird things can happen, or when calculating with the arbitrary precision type Integer, your numbers themselves will grow linearly in space.)

Re:Data structures and algorithms I actually used while work... by suyjuris on Wednesday July 15, 2020 @11:23AM (#23846524)
> I say it works better, because it has the same asymptotic runtime [...]

To be fair, while the asymptotic runtime may be the same, the C version is about 180 times faster.

Re:Data structures and algorithms I actually used while work... by eru on Saturday July 18, 2020 @04:31AM (#23879992)
Yes.

Though if we allow programs like the C example that give wrong answers or have undefined behaviour, I can write an even faster version that takes no time at all.

Re:Data structures and algorithms I actually used while work... by dotancohen on Wednesday July 15, 2020 @07:27AM (#23843720)
True, but then the "worker" fib() method should be called fib_calculate() and should be wrapped by fib() which then returns a single integer.
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @08:03AM (#23843975)
Agreed. I only left that out for brevity.
Re:Data structures and algorithms I actually used while work... by crimsonaluca20 on Wednesday July 15, 2020 @10:24AM (#23845594)
For the code above memory complexity is still O(N) even in a language that optimizes for tail calls. This occurs because you have an additional expression that occurs AFTER your recursive call that means the system must hold everything on the call stack for it to work.

Additionally you have a spelling error on line 5.

Re:Data structures and algorithms I actually used while work... by throwawayiionqz on Wednesday July 15, 2020 @07:22AM (#23843683)
The "better" recursive solution computes fib(n) and fib(n-1) at the same time. Linear time recursion!

    def f(n):
        if n<=2: return (1, 1)
        else:
            a, b = f(n-1)
            return (b, a+b)

    def fib(n):
        return f(n)[1]
Re:Data structures and algorithms I actually used while work... by dotancohen on Wednesday July 15, 2020 @07:30AM (#23843741)
You have a bug for the case of fib(0).
Re:Data structures and algorithms I actually used while work... by throwawayiionqz on Wednesday July 15, 2020 @07:34AM (#23843771)
Indeed, if you wish the sequence to be defined at 0, eru's solution is better (and appeared earlier while I was typing)
Re:Data structures and algorithms I actually used while work... by Taek on Wednesday July 15, 2020 @10:13AM (#23845478)
For Fibonacci, recursion is not the best or most efficient way to approach the problem, but I like using it as a recursion problem (artificially constrain the candidate to using recursion) because Fibonacci is so simple that you get to spend most of your time talking about recursion itself rather than talking about Fibonacci as a problem.

At least for this part of the interview, I'm not worried about your problem solving skills I'm worried about your programming fundamentals. We'll test problem solving in a different session.

Re:Data structures and algorithms I actually used while work... by Smaug123 on Wednesday July 15, 2020 @06:23AM (#23843304)
I, for one, prefer recursion to explicit iteration in most cases. Anyway, if you were going to write the answer in Haskell, it's a three-liner and it's recursive.
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @06:44AM (#23843443)
Why do you need so many lines?

    fib = 1 : zipWith (+) (0 : fib) fib
Re:Data structures and algorithms I actually used while work... by exdsq on Wednesday July 15, 2020 @07:01AM (#23843555)
Because the three liner is easier to read
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @07:07AM (#23843596)
Btw, which three-liner do you mean?
Re:Data structures and algorithms I actually used while work... by exdsq on Friday July 17, 2020 @04:04AM (#23868296)
fib 0 = 0

fib 1 = 1

fib n = fib (n-1) + fib (n-2)

Re:Data structures and algorithms I actually used while work... by eru on Saturday July 18, 2020 @04:32AM (#23879997)
Oh, that version takes exponential time to compute. The one-liner I gave computes up to the n-th Fibonacci number in linear time.
Re:Data structures and algorithms I actually used while work... by exdsq on Saturday July 18, 2020 @07:00AM (#23880520)
Your version is definitely more performant, but the three liner is just a bit nicer to read (especially for beginners) :)
Re:Data structures and algorithms I actually used while work... by eru on Saturday July 18, 2020 @07:54AM (#23880764)
Yes. It depends on what you want to show. Your version shows how to define functions and some simple pattern matching.
Re:Data structures and algorithms I actually used while work... by exdsq on Tuesday July 21, 2020 @03:40AM (#23905047)
Having worked with Haskell for a year now I still can't read much more than functions and pattern matching! :D
Re:Data structures and algorithms I actually used while work... by Smaug123 on Saturday July 18, 2020 @09:07AM (#23881167)
Not in Haskell, it doesn't. Haskell stores everything for you, right?
Re:Data structures and algorithms I actually used while work... by tome on Sunday July 19, 2020 @06:27AM (#23888372)
No, it doesn't.
Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @05:01AM (#23842812)
Because it's the most straightforward implementation in many languages?

(And you can also use recursion in the fastest implementations. You just wouldn't use the naive recursive solution.)

Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @05:18AM (#23842895)
Fastest implementation is one non-recursive equation (which I had to look up) :)

    fib(n) = (((1 + sqrt(5)) / 2)^n - ((1 - sqrt(5)) / 2)^n) / sqrt(5)
I even understood, once, how to arrive at the magic numbers :)
Re:Data structures and algorithms I actually used while work... by enriquto on Wednesday July 15, 2020 @05:57AM (#23843142)
While your formula is correct, it is not an actual implementation. How many digits of the square root do you need to compute exactly for being sure that the value of the power does not change?

Whether this is "fast" or not depends a lot on the concrete implementation of your arbitrary-precision real numbers.

As a nice thing, the second term (after the first minus sign) is smaller than 1, so you can omit it by rounding the result to the nearest integer; all the game happens in the first term.

Re:Data structures and algorithms I actually used while work... by eru on Wednesday July 15, 2020 @06:38AM (#23843401)
It depends on how you measure.

If you can do arbitrary precision arithmetic (including powers and square roots) in unit time, this one is fastest.

In practice, this algorithm is not the fastest, because handling arbitrary precision floating point numbers is a pain.

Have a look at the matrix exponentiation algorithm for Fibonacci numbers in http://pages.cs.wisc.edu/~mhock/SSL/fibcalc.pdf

You can implement the matrix exponentiation via repeated squaring recursively even in a language like Python that doesn't do tail call optimization, and it will still be fast: your recursion only goes to a logarithmic depth.

In any case, recursion vs iteration is an implementation detail. Especially if your language supports tail call optimization. Have a look at this example:

    f(0) := (0, 1)
    f(n) := let (a, b) = f(n-1) in (b, a + b)
f is recursive function over tuples of integers. And f is basically equivalent to the typically iterative algorithm for computing Fibonacci numbers.
Re:Data structures and algorithms I actually used while work... by ForHackernews on Wednesday July 15, 2020 @05:54AM (#23843128)
Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @06:13AM (#23843246)
We've learnt on "discrete math" course at university how to derive closed form solutions to any similar recursive sequences using algebra and eigenvectors.

I remember that it's possible but I forgot all the required math :) Now that I googled it it's not THAT bad

https://medium.com/@andrew.chamberlain/the-linear-algebra-vi...

Of course the only use is to look smart once a decade when the subject comes up :)

Re:Data structures and algorithms I actually used while work... by deadgunslinger on Wednesday July 15, 2020 @04:55AM (#23842782)
How do you figure from a Fibonacci exercise that the candidate understands recursion? It's 5 lines of code to memorize.
Re:Data structures and algorithms I actually used while work... by wadkar on Wednesday July 15, 2020 @05:10AM (#23842856)
By the way they discuss the implementation. First level is ensuring that base cases are covered (i.e. correct implementation of recursion)

Second level is how they explain the simple recursion that’ll hit stack limits (i.e. without tail recursion)

Third level is using accumulator/tail recursion.

See how they can express these ideas and are they able to effectively communicate their intentions.

Re:Data structures and algorithms I actually used while work... by tchaffee on Wednesday July 15, 2020 @05:53AM (#23843119)
In my decades of programming I've never had to seriously consider these issues. I have used recursion and have written some pretty deep stuff like cryptography and writing my own interpreter (for a business - not a school project). I've even implemented recursion in a language that didn't support it. I have read about tail recursion several times. I still barely remember what it is. But I know a few books to reach for if I had to use recursion again. This is why the interview process is broken. I've written lots of production code and end up being seen as a lead programmer within weeks of a new job. And your questions would make me look like I don't know what I'm talking about.
Re:Data structures and algorithms I actually used while work... by TedDoesntTalk on Wednesday July 15, 2020 @09:40AM (#23845057)
I’ve worked with some interviewers who are out to prove they are smarter than the candidate because they remember a bit of something that the candidate does not.

Only when the interviewer finds someone “smarter” than himself does he approve of hiring the candidate.

It is an ego game.

Perhaps this describes OP.

Re:Data structures and algorithms I actually used while work... by dotancohen on Wednesday July 15, 2020 @05:44AM (#23843057)
Serious request, as a developer who would probably code a naive Fibonacci that doesn't meet your standard: Could you provide a code sample that does meet your standard? This looks like an important learning opportunity for me. Thanks.
Re:Data structures and algorithms I actually used while work... by Smaug123 on Wednesday July 15, 2020 @06:20AM (#23843293)
Instead of recursing twice, recurse once, by carrying around not fib(n-1) but the pair (fib(n-2), fib(n-1)).
Re:Data structures and algorithms I actually used while work... by dotancohen on Wednesday July 15, 2020 @07:27AM (#23843713)
True, but then the "worker" fib() method should be called fib_calculate() and should be wrapped by fib() which then returns a single integer. I believe that breaks the spirit of the question.
Re:Data structures and algorithms I actually used while work... by Taek on Wednesday July 15, 2020 @09:39AM (#23845037)
Really it's about this type of discussion. Already with your 'but then' you are showing that you know your way around a program.

I'd still have you write the full thing out, but I'm guessing you'd do just fine.

Re:Data structures and algorithms I actually used while work... by Smaug123 on Friday July 17, 2020 @03:02AM (#23867927)
I'd use `go`, by ancient Haskell law and custom ;)
Re:Data structures and algorithms I actually used while work... by WJW on Wednesday July 15, 2020 @07:31AM (#23843746)
There should be a fourth level where they use the closed form solution to get to the solution they want without any recursion or looping for reasonable values of N. Maybe even a fifth level using the O(log(N)) matrix exponentiation method.
Re:Data structures and algorithms I actually used while work... by sk5t on Wednesday July 15, 2020 @08:17AM (#23844107)
I’m a bit puzzled at the idea of using recursion for an infinite sequence (did OP mean factorial?). Give me a LazyList / generator / IEnumerable instead any day for Fib.
Re:Data structures and algorithms I actually used while work... by deadgunslinger on Thursday July 16, 2020 @04:21AM (#23856927)
It's easy to memorise all those too without necessarily understanding them.
Re:Data structures and algorithms I actually used while work... by mFixman on Wednesday July 15, 2020 @06:01AM (#23843166)
I think it's the O(log n) algorithm that uses matrix exponentiation to find out the n'th Fibonacci number.
Re:Data structures and algorithms I actually used while work... by deepGem on Wednesday July 15, 2020 @05:39AM (#23843024)
I think someone's ability to understand and explain recursion like am a child is very undervalued. It's not about testing whether you know the base case or the fact that you write less lines of code in recursion but the fact that there is a built-in stack for you to use without creating one.

What are the practical applications of recursion though ? Other than sorting and DFS ( well even DFS can be done iteratively with stacks ). I'll be curious to know.

I find testing for HashTable/HashMap knowledge to be far more practical than testing for LinkedLists simply because you can't escape HashTables in today's world.

Re:Data structures and algorithms I actually used while work... by ajuc on Wednesday July 15, 2020 @07:36AM (#23843778)
> What are the practical applications of recursion though ?

Traversing and transforming nested data structures.

Just last month I had to write code that maps flat data from one system into a nested structure required by another system.

We wrote mappings as map literals and the code traverses them creating a new instance of the map filling the leaves with data from the input row and running some business logic on it.

Re:Data structures and algorithms I actually used while work... by ivanhoe on Wednesday July 15, 2020 @10:13AM (#23845475)
Seems over-complicated, e.g. I probably wouldn't go for recursion for Fibbonacci, so I'd miss your expectations completely. Why not just ask a direct question on things that you're looking for? If you care about recursion, ask them to implement simple recursive tree walker or something. Equally linked lists are something that in modern languages almost no one will have to manage by hand. If you care about understanding the concept of pointers/refs, why just not asking directly about that.
Re:Data structures and algorithms I actually used while work... by thiagocesar on Wednesday July 15, 2020 @08:18AM (#23844116)
12 years as a developer.

Used recursion only once. Once.

(but that specific time recursion was really a life saver)

Re:Data structures and algorithms I actually used while work... by LandR on Wednesday July 15, 2020 @11:08AM (#23846249)
Depends on the language.

When programming in Clojure I use recursion a LOT.

Re:Data structures and algorithms I actually used while work... by pantaloony on Wednesday July 15, 2020 @11:29AM (#23846646)
Have you tried just having a conversation about pointers and recursion? Does that not work as well (or better)?

Kudos for letting candidates know what’s going to happen, it’s just that when I see problems that are clearly just a stand in for “do you know X?” I always wonder why interviewers don’t cut to the chase and just ask directly. “Are you comfortable with recursion? Can you describe it? When does it tend to be applicable?”

Re:Data structures and algorithms I actually used while work... by ajuc on Thursday July 16, 2020 @03:58AM (#23856821)
Some people (myself included) can bullshit through such questions pretty well even when we don't know the answers. It's a skill, a combination of reading body language for clues, being confidently vague in the right places and turning attention away from dangerous questions with charisma.

You learn it quickly when teachers do oral exams at university :)

Code doesn't lie, so I understand why people like to check candidates that way instead of relying on talk alone. I would just prefer if it wasn't a hit-or-miss test on what algorithms you memorized.

Re:Data structures and algorithms I actually used while work... by watwut on Wednesday July 15, 2020 @09:16AM (#23844733)
I think that your test if fairly easy and since it requires very little time before that, I dont understand the opposition of other people.

But, you are not seeing how people think about code. You are seeing presentation prepared before hand. That whole part abour seeing how people think, as much as it is repeated, is nonsense, so maybe it is not much of loss.

Re:Data structures and algorithms I actually used while work... by theelous3 on Wednesday July 15, 2020 @06:50AM (#23843477)
Making people write code on paper is just ridiculous - way outside of testing and checking reality - and your reasoning of

>"because it shows that they know how to think about code"

means nothing at worst, and at best indicates you'll only be happy to work with people who are replicas of yourself.

Take home is the way to go, unless the position is some sort of public exhibitionist analogue developer position.

The least I think you can do is move from paper to machine, and inform them they should bring their own machine, or offer them use of one with many envs preconfigured.

Re:Data structures and algorithms I actually used while work... by toomanybeersies on Wednesday July 15, 2020 @09:25AM (#23844860)
The problem with take home interviews is that it could take one candidate an hour, and another every evening for a week.

Ideally the interviewer shouldn't be concerned about absolute code correctness, such as syntax or argument position, and should be interested more in how you're able to solve the problem, which should be a novel, yet simple task that doesn't require studying up on 200 level algorithms.

You want to hire developers (for permanent full time roles, at least) who have a good conceptual grasp of programming and logic, rather than their particular knowledge of a language or framework, or their ability to search on stackoverflow and run their code through a linter.

The worst interview problem I've been given was to write an AngularJS directive, I think literally to simply take an attribute and display it. I can't remember at the best of times the syntactical oddities of AngularJS, let alone when writing with pen and paper in an interview with no resources.

One of the better problems I was given was around parsing XML, from memory validating that opening tags matched closing tags, while allowing for self-closing tagsI remember the problem specifically didn't include any more complex facets of XML. Another was to reverse a string in-place ("hello world" -> "dlrow olleh"), and then to modify the algorithm to reverse the words but keep them in place ("hello world" -> "olleh dlrow"). Obviously in the real world you'd just use built in methods, but it's a problem that should be solvable without digging up your old lecture notes.

I've always felt comfortable writing code (or at least psuedocode) with a pen, but I think it's unfair to require a candidate to solve the problem on paper or whiteboard if they feel more comfortable typing on a computer instead, all that achieves is disadvantaging otherwise capable candidates who for one reason or another can't write code with a pen and paper. As long as they aren't googling "how to reverse a string in place", it immaterial what writing tool they use. If they feel more comfortable using a brush and papyrus, or carving a runestone, then that should be perfectly acceptable too (provided they bring their own writing material).

Re:Data structures and algorithms I actually used while work... by collyw on Wednesday July 15, 2020 @09:16AM (#23844725)
Take homes are usually very time consuming.
Re:Data structures and algorithms I actually used while work... by psalminen on Wednesday July 15, 2020 @11:04AM (#23846184)
This varies a lot. I had a take-home for a data engineer role which just asked to get and parse an xml and post a cleaned, limited json of the data. Took ~1hr. Perfect for an initial threshold.
Re:Data structures and algorithms I actually used while work... by collyw on Thursday July 16, 2020 @08:35AM (#23858333)
That's fair. Usually they involve writing some sort of miniature application. Setup of the project will usually be in the range of an hour.
Re:Data structures and algorithms I actually used while work... by blackrock on Wednesday July 15, 2020 @06:49AM (#23843469)
Are you hiring for first year beginners? Or is this also a test for your senior software engineers?

Your question topics are completely useless to normal software engineering work. And this proves how immature your company is, in understanding the nature of the work.

Perhaps you should consider asking them instead, what their technique is to ensure reliability, throughout, and redundancy in their code? Can they make their code self-heal itself? Or to make some minor decisions to restart itself, if optimal conditions are not met.

You know.. basic software engineering stuff.

Re:Data structures and algorithms I actually used while work... by rimliu on Wednesday July 15, 2020 @05:24AM (#23842927)
If you use recurssion for Fibbonacci, you do not understand neither recurrsion nor Fibbonacci.
Re:Data structures and algorithms I actually used while work... by elcomet on Wednesday July 15, 2020 @07:37AM (#23843793)
That's a bold claim. You can very much solve Fibonacci with recursion efficiently. It's just not in the naive way. (You can look up "accumulator")
Re:Data structures and algorithms I actually used while work... by rimliu on Wednesday July 15, 2020 @10:12AM (#23845461)
There are lots of thing you can, but you should not.
Re:Data structures and algorithms I actually used while work... by smabie on Wednesday July 15, 2020 @01:11PM (#23848210)
What's wrong with a memoized recursive solution? Or a DP tail recursive solution? Tail recursive solutions are less buggy, easier to code, and easier to write than iterative ones. And they're much easier to prove the correctness of as well.
Re:Data structures and algorithms I actually used while work... by Jtsummers on Wednesday July 15, 2020 @02:07PM (#23849174)
The memoized recursive solution is a good demonstration of how to use memoization to improve recursive function execution time. But it also seriously increases the memory requirements for many algorithms. If the question is "generate the first N numbers in the Fibonacci series", then memoization is a reasonable answer (though still not the fastest) since you actually need to generate and store every value anyways. If the question, instead, is "generate the Nth number in the series", then memoization will require an array of N values, when only 2 (at a time) are needed (which can still be generated recursively, but efficiently):

  (defun fib (n &optional (a 0) (b 1))
    (cond ((zerop n) a)
          (t (fib (1- n) b (+ a b)))))
With tail recursion, that should be as fast as the iterative solution and uses as much memory to store the intermediate values.

Of course, the Fibonacci series also grows incredibly fast, so practically speaking, unless your language supports arbitrarily large integers, you'll never generate even 100 elements of the series.

Re:Data structures and algorithms I actually used while work... by jlokier on Wednesday July 15, 2020 @02:49PM (#23849844)
If the question is "generate the Nth number in the series", I would be inclined to suggest a closed-form solution that returns in O(1) time (assuming regular size integers), no iteration required.
Re:Data structures and algorithms I actually used while work... by WalterBright on Wednesday July 15, 2020 @05:32AM (#23842986)
"You, Sir, are employing a double negative." -- Mr. Spock
Re:Data structures and algorithms I actually used while work... by rimliu on Wednesday July 15, 2020 @05:46AM (#23843072)
Am I?
Re:Data structures and algorithms I actually used while work... by WalterBright on Wednesday July 15, 2020 @06:05AM (#23843185)
It's "not understand either" or "understand neither".
Re:Data structures and algorithms I actually used while work... by c17r on Wednesday July 15, 2020 @06:06AM (#23843195)
“You understand neither recursion nor Fibonacci”

Or

“You do not understand recursion nor Fibonacci”

Re:Data structures and algorithms I actually used while work... by egman_ekki on Wednesday July 15, 2020 @07:17AM (#23843654)
Ain't nobody got time for that.
Re:Data structures and algorithms I actually used while work... by WalterBright on Wednesday July 15, 2020 @06:48PM (#23853301)
Ain't nobody got no time for that.

FTFY

Re:Data structures and algorithms I actually used while work... by P-ala-din on Wednesday July 15, 2020 @07:41AM (#23843819)
I feel like these are poor choices.

* linked lists shouldn't be used anyway. and if they are used, you should use the standard implementation

* directly applying fibbonacci is trivial.

* sort: This is just a memorisation task, what's the point? (99% of the time, you shouldn't implement your own sort)

although, I agree with the open-book approach in the problem solving part.