views:

2915

answers:

18

I teach a software engineering lab class for Seniors in Computer Science and Engineering (Bachelor's degree). I understand that many companies have interviewees solve simple programming problems on a whiteboard or piece of paper.

I would like to prepare my students for this as they do not normally have to stand in front of a judgmental audience and write programs on a whiteboard. Thus, I am looking for some good questions to ask them.

The question should be answerable in 15-30 minutes, on a whiteboard, by a solid CSE graduate. I am looking for programming questions, not puzzles.

A: 

Write a function which counts the number of vowels and consonants in a string. Any language you choose.

dacracot
Trick is not to forget upper and lower, non-letters, and finally a null string.
dacracot
I'd use a regex to substitute the letters, then split it into an array, and get the occurences of my strings :D
Mez
what about sometimes "Y"? :)
mike511
A: 

Write pseudo-code that would sort an array using several different algorithms.

Dan Cramer
Probably not a 15 minute board question, though. Maybe just bubble sort.
Adam Davis
+2  A: 

Classic c style string questions:

  1. implement strstr,strrchr, strpbrk, strlen functions and all other string manipulation: Contains, Replace, etc... Performance wise.

  2. implement strtok. Implement it as thread safe.

Knowing pointers is a basic test. Experienced programmer should be able to do it within 15 minutes.

Yuval Peled
+2  A: 

There are quite a few sites dedicated to this. e.g., http://www.techinterviews.com/. Most of them have puzzle questions, but they also have real technical questions as well. I'd check out those if you want a big list. There are hundreds of good questions in lots of different areas.

Derek Park
+11  A: 

After years of trying various approaches to this problem in job interviews, I have settled on what I think is the best challenge:

strstr: find the first location of a string in another string.

This is a well-known function across many languages, and can be coded in under fifteen lines. (Anything more is too much for a whiteboard.)

strstr is also perfect because it is just the right level of difficulty: two levels of looping. It sounds like it might be too easy, but it will instantly separate those who can code from those who cannot. In fact, maybe only one in five candidates gets it right without help.

adum
+2  A: 

Implement a simple FIFO (queue) and LIFO (stack).

Implement a priority queue.

Implement a simple linked list.

Implement a whitespace stripping function - removes leading and/or trailing whitespace from a byte array.

Implement a function to convert a positive number into its hexadecimal equivalent, and then the converse.

Implement atoi.

Write a some code which demonstrates limitations of floating point code.

Write a string compare routine in a recursive function.

Adam Davis
Adam, a stack is not a type of queue. Queues are FIFOs.
Derek Park
Not all queues are fifos - a priority queue is still a queue, but is not a fifo. You are correct that stacks are not queues, which is a mistake on my part. Fixed the text. Did someone downvote this just for that one error? Wow, strict types we've got around here!
Adam Davis
Your text is still confusing. A stack is a LIFO. A queue is not. The standard queue is a FIFO. Priority queues are different from standard queues, and are still not LIFOs.
Derek Park
Doh! I edited incorrectly! My bad...
Adam Davis
+3  A: 

A good one I've used before is a simple question that reveals code reading skills, knowledge of binary, inductive reasoning skills, and communication skills (as the answer is probably not obvious, and questions are encouraged)

What useful question does the following function answer about a number? Yes, it is a useful and distinct quality for a number to have, especially so in computer science. (Please tell them to ask questions and walk themselves through their thought process on the whiteboard)

bool mystery(unsigned int num){
    unsigned int other = num - 1;
    bool result = ((num & other) == 0);
    return result;
}

The answer will be in a comment to this post.

Sufian
This function answers the question: Is this number a power of two? (Bonus points if they realize it is incorrect for the input number 0)
Sufian
+4  A: 

I tend to ask questions that should be solvable within a few minutes so that there is time for Q&A on their implementation. One question I like to ask is:

Write a function that counts the number of bits in a list of integers.

I'm often surprised on how many people have trouble with this.

Rontologist
+1  A: 

One I use a lot is to write a function that reverses the words in a sentence, excluding punctuation. There are several variations on this by allowing/not allowing built-in methods for string manipulation.

So, the sentence "The quick brown fox" becomes "fox brown quick The".

The more important part of these excercises is to see/hear how the interviewee reasons out the solution. There is usually more than one way to solve the problem none of which are "more right" than any others.

Scott Dorman
+2  A: 

For OO languages:

Implement a money class that:

  • Doesn't suffer from precision loss (big numbers = bad juju in floating point)
  • Stores money internally in US dollars
  • Implements methods that return the value of money in Euros, Yen, Pounds, etc

Implement a roman numeral class (accepts roman numeral strings or integers, stores the number, returns integer or roman numerals, doesn't have to be perfect (say only for numbers up to C)

Adam Davis
+1  A: 

Having your students create simple functions on a whiteboard is good practice however I have noticed (at least for the company I work for) when we interview someone the problems they need to solve are much more involved.

The main goal really for us is to see someones approach to an overall problem and how they solved it. For example:

  1. Implement or design a data base structure for a simple login application
  2. Given an xml structure, design classes and parsers to consume and manipulate the xml

All not very difficult problems to solve but they help the interviewer learn how the interviewee approaches a problem, cause once the interviewee has finished the problems or has attempted them, we would discuss their design and approach.

Just food for thought, possibly a good exercise would be to have them explain their approach to solving the problem once they have finished. This also gives them a chance to rethink about the problem while they are explaining it and helps reinforce good communication skills.

JustFoo
+1  A: 

The first time I had to do whiteboard coding in an interview I totally flubbed it. My experience was that I was completely unprepared to write (compilable!) code on a blackboard without practice, and any kind of practice would've been an immense help.

Anything simple,a few loop iterations. The important thing is to make it compilable.

Steve B.
+1  A: 

Write pseudocode and the sketch of a design for a search engine. What are the parts of the system? How do you define success? What are the different phases of operation?

You can deep-dive into any part of this thing to see if the person answering the question has the chops.

djsadinoff
+1  A: 

Implement Twitter or iGoogle from scratch, go. (an idea from @rex-m)

Chris Ballance
+2  A: 

The number one way I eliminate candidates is by very basic SQL experience. To that end I ask a couple of what should be simple SQL questions.

  1. requires them to know how to do a simple select statement with a where clause.
  2. requires them to do an inner (and outer) join on two tables
  3. requires them to update records with a where clause.

Unfortunately, 90% of the so called application programmers out there can't do those queries. I can understand an embedded systems dev not knowing them, but anyone with a modicum of experience doing client / server or web apps should be able to answer them quickly.

Also, I don't even care about exact syntax, as long as they understand the idea.

Chris Lively
+2  A: 

Here are some I've been asked in interviews:

Write a function that does integer division without using / or %.

Given two nodes in a binary search tree sketch a function that finds their least common ancestor.

"Programming Pearls" by Jon Bentley is a great resource for these types of puzzles. Also "Programming Interviews Exposed" is a good resource.

MrDatabase
+2  A: 

You may want to try a few problems from Project Euler they're fairly math oriented, but they can be quite challenging and rewarding.

Zee JollyRoger
+1  A: 

Implement a binary search (presuming a sorted input).

Most people get it wrong the first couple times through - I only ever use an already-written and -verified binary search because I get it wrong usually the first two times, too :)

warren
+1 Just conducted a half dozens interviews. None of the candidates answered the question smoothly. *"Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky."* — Donald Knuth.
FM
@FM - in 'Programming Pearls 2d ed', the author notes there was an error in the first edition with regards to binary search - and he is a pro :)
warren