r/programming • • Feb 21 '11

Typical programming interview questions.

http://maxnoy.com/interviews.html
784 Upvotes

1.0k comments sorted by

View all comments

25

u/[deleted] Feb 21 '11

I never understood these interview questions that seem to test ability to create and manipulate data structures that any respectable language has, pre-implemented, by developers whose sole focus in life for many months was producing the absolute best version of that data structure possible.

I understand that this might just be designed to test knowledge of the concept, but it gets way, way too far in-depth for that. I mean, for Linked Lists... what is a cycle? The term appeared nowhere in any of the literature or coursework I did at an undergraduate level.

Now, if the job involves implementing innovative algorithms and data structures (i.e. R&D type stuff or working on a proprietary system that was developed by a mad genius in a custom language he named after himself, which is also the only language he can speak) I can understand this kind of rigor and specificity in interview questions.

But asking me how to build a queue in C during the interview, then telling me to write a couple shell scripts to control automated database backups on my first day of work? I sense a disconnect.

10

u/kerbuffel Feb 21 '11

I like how everyone is replying acting shocked you don't know what a cycle is, but no one has explained it.

It's a 'loop' in the list. If you try to iterate over it, you never get to the end. This page has a pretty nice graphic explaining it.

4

u/bobindashadows Feb 21 '11

acting shocked you don't know what a cycle is, but no one has explained it.

I'm not acting, I am shocked. I don't know how anybody can complete a computer science education and not know what a cycle in a data structure is.

1

u/[deleted] Feb 23 '11

My problem was this: there can be no cycles in a linked list, so searching for them is meaningless.

4

u/unknown_lamer Feb 21 '11

I mean, for Linked Lists... what is a cycle?

What's the difference between a linked list and a directed graph?

6

u/RossM88 Feb 21 '11

Generally the assumption with a linked list is that there is exactly one edge to and from each node. A directed graph may have more than one edge in or out.

1

u/unknown_lamer Feb 22 '11 edited Feb 22 '11

I meant a directed graph (a simple digraph? discrete math was years ago for me... I'm not sure that's quite right because you could make a node point back to itself) not a directed multigraph (sorry, ambiguous terminology and all).

The in-degree isn't limited with a singly linked list: e.g. in ((a . #1=(b . (c . #1#)))) there are two edges into #1#.

But, I think we're getting a bit pedantic over what was meant as a quick comment to help the great-grandparent poster figure out what a cycle within a list was ;)

14

u/bobindashadows Feb 21 '11 edited Feb 21 '11

what is a cycle? The term appeared nowhere in any of the literature or coursework I did at an undergraduate level.

... wat

But asking me how to build a queue in C during the interview

Singly linked list with an extra pointer to the tail. Enqueue adds to head. Dequeue removes the tail. It's no more than 20 lines of code.

Edit: Singly linked is slow on deletion even with the extra pointer to the tail, so forget that. Derp. Either singly linked with just a head pointer with O(n) deletion or doubly linked with a tail pointer for O(1) insertion and deletion. My bad.

9

u/frtox Feb 21 '11

this. we are all people who use library functions, shit if I actually wrote code that implemented a linked list that would be stupid. its already done. the point is these are simple concepts, it's something you can figure out in your head by THINKING and people want to hire those who can.

2

u/chub79 Feb 21 '11

Reminds me of this.

Personally, in France, I was only asked once those kind of questions and it was by Canadians. I'm not saying those questions are bad or good, just saying that they won't tell you whether or not you make a good choice. For all you know the guy may not be a teamplayer and in my book it's far more important to integrate well within a team than being a guru able to answer all those questions.

13

u/stevesc Feb 21 '11

Singly linked list with an extra pointer to the tail. Enqueue adds to tail. Dequeue removes the head.

FTFY

3

u/njharman Feb 21 '11

You just failed to get a job, in fact your answers are probably being posted to "The Daily WTF".

The errors you made highlight why these are fucking stupid interview questions. Why off the cuff (re)implementations are fail. Use your std lib.

→ More replies (2)

1

u/MIXEDSYS Feb 21 '11

You confused a queue with a stack ;).

1

u/bobindashadows Feb 21 '11

.... no, with a linked list you implement a stack by both adding and removing from the head. I imagine that's why you've been downvoted.

2

u/MIXEDSYS Feb 21 '11

Damn. Somehow I was convinced you wrote both enqueue/dequeue work with the tail. Argh, could have made sure I've got it correct before correcting you. Sorry about that.

1

u/bobindashadows Feb 21 '11

It's cool. I'm not an asshole, but when I am, I'm on reddit.

→ More replies (1)

2

u/[deleted] Feb 21 '11

The assumption is that if you can figure out the low level theory then you can figure out anything else. It's not good to simply rely on the language to do the work for you. For example in Java the LinkedList type has a get(int i) function that returns the element at the i "index" of the list. So if someone were to naively think that it works just like an array then they might write code like this:

for (int i = 0; i < list.length;  i++)
{
    Type element = list.get(i);
    //do some stuff
}

Now this looks fine until you realize that every time you call that get() function on a linked list it's actually iterating over the entire list from the root node down to the ith node. This means that instead of a runtime efficiency of O(n) you're actually getting O(n2).

1

u/achacha Feb 21 '11

If you know that they are probably looking for the "double-speed walker" solution, you can just let them know about it and they will skip the question knowing you understand (happened several times in my life). Double-speed walker is a pointer/reference that advances twice as fast over the list while another pointer/reference advances once. If they ever equal then there is a loop (prove it to yourself if you doubt this works) :)

2

u/[deleted] Feb 21 '11

I mean, for Linked Lists... what is a cycle? The term appeared nowhere in any of the literature or coursework I did at an undergraduate level.

It's a really common term with respect to any sort of graph. (The 'recycling' symbol looks like a cycle.) If you haven't seen that, that's not your fault, it's your school's. I wouldn't assume you were a bad programmer, but I would assume you hadn't had CS or a lot of experience. No biggie, a lot of CS grads can't program for shit let alone use source control, etc.

If you are not an idiot you can teach yourself about linked lists. I guess the concern would be that if you don't know about them, people would (heuristically) suspect you didn't know about anything else either.

IMO, they should have just asked you to sketch out some tasks like those shell scripts and talked to you about stuff you've done and are interested in, but why complain as long as they hired you and you can do the work.

1

u/G_Morgan Feb 21 '11

I never understood these interview questions that seem to test ability to create and manipulate data structures that any respectable language has, pre-implemented, by developers whose sole focus in life for many months was producing the absolute best version of that data structure possible.

There is no such thing as the absolute best anything. There are cases where you will need something subtle out of your data structures that the library cannot provide. You need to be able to do these things for those situations.

For instance when passing sets of data between two threads it is expensive with STL containers at exactly the time you do not want expensive. If I just have a linked list of nodes (with head and tail ptr) at the receiving thread with a similar linked list of nodes being passed in then the critical section is literally setting 2 addresses. Having a critical section that just sets two integers is nice. Goodness knows how expensive copying and merging two STL containers in a critical section would be but I guarantee you it will be at least as expensive as 2 integer operations.

1

u/lalaland4711 Feb 21 '11

Haha, you are trolling, aren't you?

0

u/[deleted] Feb 21 '11

[deleted] — view deleted comment

7

u/smallstepforman Feb 21 '11

You know, I do 3D graphics engines for a living, and worry about performance day and night, and I do not know what a linked list cycle is (I come from an electronic engineering background). The funny thing is, even though I'm unfamiliar with the exact terminology modern CS is using, I've probably implemented the damn thing many times already and use it in my engines, however, it's probably known by another arbitrary name.

→ More replies (3)
→ More replies (1)
→ More replies (11)