Recently I have been interviewing people for the post of software engineer. We were looking for fresh developers so most of the candidates were students which graduated recently or were graduating in a month or so. Asking all sort of language questions seem very odd for them as they don’t have any professional experience. The criteria I set was to judge how smart they are and then ask them some programming question which they had to implement in their favorite programming language. I don’t check for syntax errors as I said earlier the criteria is to judge how smart they are and how efficient and optimized solution they can come up with. So here are some of the questions I usually ask:

- Given a sorted array of integers of length N containing different integers. The array is sorted in ascending order and can have duplicates. The developer needs to come up with the most efficient (for both speed and memory) solution which returns an array containing integers with duplicates removed.

For example if the array is:*int[] array = new int[] {1, 2, 2, 3, ,4, 5, 5, 6}*the resulting array will be*{1, 2, 3, 4, 5, 6}* - Given an array of 1001 elements which contains integers from 1 to 1000 inclusive. The numbers are randomly stored in the array. Only one number repeats itself. The candidate has to come up with an efficient solution for finding that duplicate given that you can access the elements of an array only once i.e., you can read the elements of the array only once.
- This one’s my favorite. I usually ask this to experienced developers in order to judge how good they are at tackling difficult problems. Here is the question. We need to store a family tree in the database. The schema should be able to store the name, sex and relationship of each family member. Based on the schema the candidate produces I ask them to write some queries like:

return all the family members who don’t have any children or return all the children of some family member name that I give. This will include the children of children also. - We want to implement an election algorithm. The input will be an integer N which specifies the number of people from which we need to elect the person. Lets assume that all people are standing in a circle. The algorithm is to eliminate every alternate person and continue in this fashion until we have one person left. The best method for its implementation is to use a circular linked list. The candidate needs to write the code to initialize this linked list and then write the code for the algorithm.
- This is one of the toughest questions I have in my arsenal. The candidate needs to implement the perfect shuffle algorithm. The candidate can use a special function
*int rand(int n)*which returns any number from 0 to (n – 1) inclusive. The implementation of the algorithm uses some sort of*swap(int a, int b)*function which every one knows uses 3 operations to swap the two numbers. I than ask the candidate to optimize the code by implementing this swap function in two operations. This swap function doesn’t have to be a separate function, and any changes to the whole implementation is OK as long as you can come up with an optimized solution. - The candidate needs to write a code for reversing a word given as input to the function. The signature of the function is: String
*reverse(String inputString)*After the candidate has successfully implemented the code I ask the real question. Which is to come up with a sentence reverse function which reverses all the words in the sentence i.e., if the input sentence is “**The quick brown fix jumped over the lazy dog**”, the output will be “**dog lazy the over jumped fox brown quick The**”. Confused? Just concentrate on how can you use the*reverse*function to implement the solution.

These are some of the question I usually ask. There are many more that I have. In the next post I will post the solutions that I think are the best but the readers are welcome to post solutions to these problems.

[…] an interesting set of interview questions on programming in this blog: Given an array of 1001 elements which contains integers from 1 to 1000 […]

These questions are excellent. Similar to them, good collection of core java interview questions are available at javapapers.com

Question 5. Once somebody asked me to write such function/application. I found a solution with no swap. Instead of shuffling the deck, I do generate the deck in random order. To tell the truth, interviewer was not ready to such solution and I passed.

U promised the solutions ….where r they??