Programming Interview Questions

The complete list of all my programming interview question articles with pointers to original posts. There are 28 questions in total, and since 28 is a perfect number (as Donald Knuth also mentioned) I decided that’s a good place to stop.

1. Array Pair Sum
Given an integer array, output all pairs that sum up to a specific value k.

2. Matrix Region Sum
Given a matrix of integers and coordinates of a rectangular region within the matrix, find the sum of numbers falling inside the rectangle. Our program will be called multiple times with different rectangular regions from the same matrix.

3. Largest Continuous Sum
Given an array of integers (positive and negative) find the largest continuous sum.

4. Find Missing Element
There is an array of non-negative integers. A second array is formed by shuffling the elements of the first array and deleting a random element. Given these two arrays, find which element is missing in the second array.

5. Linked List Remove Nodes
Given a linkedlist of integers and an integer value, delete every node of the linkedlist containing that value.

6. Combine Two Strings
We are given 3 strings: str1, str2, and str3. Str3 is said to be a shuffle of str1 and str2 if it can be formed by interleaving the characters of str1 and str2 in a way that maintains the left to right ordering of the characters from each string. For example, given str1=”abc” and str2=”def”, str3=”dabecf” is a valid shuffle since it preserves the character ordering of the two strings. So, given these 3 strings write a function that detects whether str3 is a valid shuffle of str1 and str2.

7. Binary Search Tree Check
Given a binary tree, check whether it’s a binary search tree or not.

8. Transform Word
Given a source word, target word and an English dictionary, transform the source word to target by changing/adding/removing 1 character at a time, while all intermediate words being valid English words. Return the transformation chain which has the smallest number of intermediate words.

9. Convert Array
Given an array [a1, a2, ..., aN, b1, b2, ..., bN, c1, c2, ..., cN]  convert it to [a1, b1, c1, a2, b2, c2, ..., aN, bN, cN] in-place using constant extra space

10. Kth Largest Element in Array
Given an array of integers find the kth element in the sorted order (not the kth distinct element). So, if the array is [3, 1, 2, 1, 4] and k is 3 then the result is 2, because it’s the 3rd element in sorted order (but the 3rd distinct element is 3).

11. All Permutations of String
Generate all permutations of a given string.

12. Reverse Words in a String
Given an input string, reverse all the words. To clarify, input: “Interviews are awesome!” output: “awesome! are Interviews”. Consider all consecutive non-whitespace characters as individual words. If there are multiple spaces between words reduce them to a single white space. Also remove all leading and trailing whitespaces. So, the output for ”   CS degree”, “CS    degree”, “CS degree   “, or ”   CS   degree   ” are all the same: “degree CS”.

13. Median of Integer Stream
Given a stream of unsorted integers, find the median element in sorted order at any given time. So, we will be receiving a continuous stream of numbers in some random order and we don’t know the stream length in advance. Write a function that finds the median of the already received numbers efficiently at any time. We will be asked to find the median multiple times. Just to recall, median is the middle element in an odd length sorted array, and in the even case it’s the average of the middle elements.

14. Check Balanced Parentheses
Given a string of opening and closing parentheses, check whether it’s balanced. We have 3 types of parentheses: round brackets: (), square brackets: [], and curly brackets: {}. Assume that the string doesn’t contain any other character than these, no spaces words or numbers. Just to remind, balanced parentheses require every opening parenthesis to be closed in the reverse order opened. For example ‘([])’ is balanced but ‘([)]‘ is not.

 15. First Non Repeated Character in String
Find the first non-repeated (unique) character in a given string.

16. Anagram Strings
Given two strings, check if they’re anagrams or not. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and capitalization. Each letter should have the same count in both strings. For example, ‘Eleven plus two’ and ‘Twelve plus one’ are meaningful anagrams of each other.

17. Search Unknown Length Array
Given a sorted array of unknown length and a number to search for, return the index of the number in the array. Accessing an element out of bounds throws exception. If the number occurs multiple times, return the index of any occurrence. If it isn’t present, return -1.

18. Find Even Occurring Element
Given an integer array, one element occurs even number of times and all others have odd occurrences. Find the element with even occurrences.

19. Find Next Palindrome Number
Given a number, find the next smallest palindrome larger than the number. For example if the number is 125, next smallest palindrome is 131.

20. Tree Level Order Print
Given a binary tree of integers, print it in level order. The output will contain space between the numbers in the same level, and new line between different levels.

21. Tree Reverse Level Order Print
This is very similar to the previous post level order print. We again print the tree in level order, but now starting from bottom level to the root.

22. Find Odd Occurring Element
Given an integer array, one element occurs odd number of times and all others have even occurrences. Find the element with odd occurrences.

23. Find Word Positions in Text
Given a text file and a word, find the positions that the word occurs in the file. We’ll be asked to find the positions of many words in the same file.

24. Find Next Higher Number With Same Digits
Given a number, find the next higher number using only the digits in the given number. For example if the given number is 1234, next higher number with same digits is 1243.

25. Remove Duplicate Characters in String
Remove duplicate characters in a given string keeping only the first occurrences. For example, if the input is ‘tree traversal’ the output will be ‘tre avsl’.

26. Trim Binary Search Tree
Given the root of a binary search tree and 2 numbers min and max, trim the tree such that all the numbers in the new tree are between min and max (inclusive). The resulting tree should still be a valid binary search tree.

27. Squareroot of a Number
Find the squareroot of a given number rounded down to the nearest integer, without using the sqrt function. For example, squareroot of a number between [9, 15] should return 3, and [16, 24] should be 4.

28. Longest Compound Word
Given a sorted list of words, find the longest compound word in the list that is constructed by concatenating the words in the list. For example, if the input list is: ['cat', 'cats', 'catsdogcats', 'catxdogcatsrat', 'dog', 'dogcatsdog', 'hippopotamuses', 'rat', 'ratcat', 'ratcatdog', 'ratcatdogcat']. Then the longest compound word is ‘ratcatdogcat’ with 12 letters. Note that the longest individual words are ‘catxdogcatsrat’ and ‘hippopotamuses’ with 14 letters, but they’re not fully constructed by other words. Former one has an extra ‘x’ letter, and latter is an individual word by itself not a compound word.

VN:F [1.9.22_1171]
Rating: 9.9/10 (55 votes cast)
Programming Interview Questions, 9.9 out of 10 based on 55 ratings

This entry was posted in Programming Interview. Bookmark the permalink.
  • EquinoX

    by STOP … you mean you will stop posting only programming interview questions ?? but you can still post on game theory,network programming,ethical hacking,android programming etc(if you have experience in those fields).I like your posts bcoz they have a constructive approach … plzz dont limit them only to the domain of interview questions.Hope you change your mind :)

    • Arden

      Thanks for the encouraging comment. Yes I’ll ONLY stop posting interview questions, and continue to write about Machine Learning. My area of focus is Information Retrieval and Machine Learning, and I will start sharing more in these areas. In fact I originally started this blog about ‘how to implement a search engine’. You can find those articles here. Happy to hear that you enjoy my blog..

      • http://kartiksura.blogspot.in/ Kartik Sura

        Your blog is one of the best blogs for interview preparation.
        And your problem solving approach is awesome.
        Please continue your good work.. :)

      • Karl Grissom

        I got to agree with Kartik, I really like this collection or problem solving questions.
        If by any chance you continue to work on it then here is a site with programming tests which could potentially inspire you.
        They have a set of problem solving questions, but also they provide a bug fixing questions which are very interesting.

  • EquinoX

    can u suggest some books for developing complexity efficient algorithms and using data structures for solving interview problems ? coz i need some serious help for my interview preparation … thanks

    • Arden

      I personally used ‘Cracking Coding Interviews’ book extensively. It’s a great resource to prepare for programming interviews. The author also maintains a very helpful interview questions forum careercup.com, where you can find thousands of questions and answers that major tech companies such as Microsoft, Google, Apple, Amazon… ask to their candidates in real interviews.

      ‘Algorithms for Interviews’ is also extremely useful. It’s somewhat more advanced, but I would definitely recommend. It contains many wonderful questions and elegant solutions.

      ‘Programming Interviews Exposed’ is more basic and focuses mostly on fundamentals. It’s a good resource if you need to refresh the main concepts.

      ‘Algorithm Design Manual’ is an excellent data structures and algorithms book. I used it a lot to study data structures, algorithm analysis, sorting algorithms, graphs etc. If you have limited time to study I would strongly recommend this one.

      These are the primary resources that come to my mind, I hope it helps. Best of luck in all your upcoming interviews..

  • Olcay

    Hi Arden,

    I truly appreciate all the resources you shared. Such a great collection of study materials that are very well organized and presented. Cok tesekkurler.

    • Arden

      Thanks a lot for the compliments Olcay. I’m very glad that the articles are helpful. Rica ederim..

  • Cmon

    Hungry for more!!!
    Some more questions please!!!

  • Waqas Ahmad

    Hello Arden,

    Could you please mention some good books on c#.

  • Anonymous

    You are awesome. Gr8 work :)

  • Vijay13

    Awesome !! Awesome !! Awesome !! Awesome !! Awesome !!

    Some more question-answers PLEASE……

  • sarah

    You are just AMAAAZIIIIING :)

  • Urvashi Pathak

    Write a function evenodd() to check whether a given number is odd or even. The function must return 1 if the number is even, return zero if the number is odd. Write a main function which reads the matrix of order MXN , counts the total even numbers in the matrix and also displays the total even numbers. i need to know the coding for this…

    • Ravi

      Always use bit operation to find even or odd.
      (number ^ 1) == 0

  • nethra

    longest increasing subsequence plz…..

    • Vikram N

      Have to modify the code for ‘Longest Common Subsequence’ a bit.

  • Parad0X

    This is great!

  • Ashok

    How to reverse a string and when reversing it shouldn’t have duplicates

  • Xiang

    Good books for Google/Amazon Interview(onsite)

    Apart from CLR and Skiena’s algorithms book, I would recommend the
    following books focussed on real coding interviews questions which
    contains great details(real questions with real answers as expected by
    the google/amazon interviewer):

    *Top 10 coding interview problems asked in Google with solutions: Algorithmic Approach By Dr Lin Quan

    *Elements of Programming Interviews: 300 Questions and Solutions By Adnan et al.

    *Programming Pearls By Dr Bentley

    *More Programming Pearls By Dr Bentley

    These books helped me greatly in my Amazon’s onsite interview recently.

    Practicing topcoders questions can be an additional help if it suits individual interest.

    • geekguy

      add Cracking the coding Interview ! :)

  • dookerj

    Awesome post! Wish there were Ruby examples, though

  • Vikram N

    Arden,

    I am preparing for a technical interview and I am glad I found this one! This is really good collection of Q&A’s.

    God bless you!

  • Aakash Anuj

    In C++:

    #include

    using namespace std;

    int main() {

    // your code goes here

    string s;

    cin>>s;

    int array[26];

    int i;

    for(i=0;i<26;i++)

    {

    array[i]=0;

    }

    for(i=0;i<s.length();i++)

    {

    char c;

    c=s[i];

    int ascii=(int)c;

    array[ascii-97]++;

    }

    for(i=0;i<s.length();i++)

    {

    char c;

    c=s[i];

    int ascii=(int)c;

    if(array[ascii-97]==1)

    {

    cout<<c<<endl;

    break;

    }

    }

    return 0;

    }

  • Anish Rushi

    Check for more IT Technical interview questions, aptitude questions @ http://skillgun.com

  • Sreenath Reddy
  • zenlotus

    good

  • zenlotus

    good job!