Programming Interview Questions and Answers
There is no dearth of programmers and computer science graduates applying for the positions of a programmer at different levels in an organization. Big Technology giants like Amazon, Google, Microsoft are always on the lookout for programmers with in-depth knowledge in key areas. Even service-based companies like Uber, Netflix encourage talent in coding.
The challenge is to cruise past the most difficult interviews in these companies. Here we have put together top 15 Programming Interview Questions which may be thrown at the interview.
To best address all aspects of programming, the questions must be divided into various sections:
Interview Questions on Arrays
Arrays are the most frequently encountered topics in coding and there are plenty of questions that can be asked on this. Below are the most frequently asked ones:
1. How will you find a missing number in an integer array of say 1-100?
This is a common array question asked in Java, C or C# interviews. There are multiple approaches to tackling this question:
• You can use the formula for sum of the series which is n(n+1)/2. But this will work only if there is one listing number.
• BitSet class in Java if the array has more than one missing number.
2. In an array, there are multiple duplicate numbers. How will you find them?
The trick to approaching this question is to use Hashmap or HashTable to store the number as a key and number of occurrence as its value. If the number is already present in the table, then increment the value or insert 1. Later, print out all those numbers who have more than 1 as the value.
3. Given two arrays, find which number is not present in the second array.
For this again, HashTable comes in handy. Put the elements of the second array in the HashTable. For every element in the first array, check if there is an entry in the HashTable. Output all elements of the first array that are not there in the HashTable.
4. How will you remove duplicate elements from an array in Java?
For this, the tricky part is not just finding the duplicates but also remove them. For this you can use Collection Class unless the interviewer says specifically not to use it. You need to convert your array to an ArrayList and then create a LinkedHashSet from that ArrayList.
In this way, duplicates are removed from the array without copying them to a result array.
You may also like: Tips to Stay Focused in an Interview
Interview Questions on Strings
String class is a key element in any programming language and there are always some questions on String variables. Some of them are:
1. Write code to check whether a given String is a palindrome or not
These are the steps to check if a String is a palindrome or not:
• Reverse the given string.
• Check if the reversed string is equal itself. If yes, then the string is a palindrome. The reverse(String text) method is used for the same. If the result of the reverse() method is equal to the original string, then the given string is a palindrome.
2. How will you extract a particular character from a string?
Convert the string into a character array and then use substring() method to remove the particular character from the string.
3. Differentiate between String, StringBuilder and StringBuffer in Java
String in Java is immutable whereas StringBuilder and StringBuffer are immutable. StringBuffer is synchronized whereas StringBuilder is not and hence used for temporary string manipulation because it’s faster.
Interview Questions on Linked List
Apart from String and Array, Linked List is another kind of data structure that is very important in any programming language. It can do whatever array cannot. Below are some common Linked List interview questions.
1. How will you find the middle element of a Linked List in a single iteration?
The simple solution for this is to traverse the Linked List until you reach the tail that points to null, find the length of the Linked List and keep re-iterating till you reach the middle.
To find it in a single pass, you must use two pointers-one incrementing one step at a time while other increments two steps at a time. While one pointer reaches the end of the list, the other will point to the middle element.
2. How will you find out if there is a loop in a single linked list?
This question can be answered can be solved using 2 pointers- increase one pointer one step at a time and the other one two steps at a time. This way, if a loop exists, they will meet each other at some point.
Some other frequently asked Programming interview questions
For the below questions, you will need to write code or algorithm.
1. How will you implement a binary search tree?
2. Using quick sort, write a program to sort numbers.
3. Write code to implement a binary search algorithm (in Java or C++)
4. Write a program to find all prime numbers up to a certain number
5. Write the algorithm to find the nth Fibonacci number in a series.
The key pointer in a programming interview is that the interviewer will go with the flow and ask you follow up questions based on your response. You must be sharp and attentive. While writing code, you must account for nil input, erroneous input and other checks.
More than the exact syntax, the interviewer will rate you based on your quick response, problem-solving skills and ability to think out of the box. If you are well prepared and confident, no one can stop you from landing your dream Programming job.
Looking for Programming Jobs in top cities? Click at the links below:
• Programming Jobs in Bangalore
• Programming Jobs in Mumbai
• Programming Jobs in Chennai
• Programming Jobs in Gurgaon
• Programming Jobs in Pune