# Top 10 Programming interview questions in 2018

An increasing number of students are opting for IT careers each year, since most of the largest companies in the world are from the tech industry. There is an ever-increasing number of computer science/software engineering graduates who are looking for jobs, but unable to secure good employment opportunities due to a lack of initial experience.

Acing an interview, especially at the beginning of a career, is perhaps the most important and challenging part of a software developer’s life.

In this article, we will take a look at the top 10 most popular **programming interview questions** that are asked in the industry. These questions are from interviews for programmers at different experience levels from fresh graduates to those who have a few years of experience under their belt.

**Note**: Interview questions at this stage are often generic, and not geared towards a specific technology. They are designed to test a candidate’s programming concepts and problem solving skills. If you are appearing for a technology or language specific interview, learning the answer to these questions can still be beneficial as the same concepts apply to nearly all programming languages.

### 1. The palindrome check

**Question**: Check to see if a given string is a palindrome or not.

**Solution**: A palindrome is a string that is the same even when reversed. An example is the word “civic”. Simply reverse the input string, and then compare with original to see if it is a palindrome or now. Here’s a basic C++ implementation.

int flag = 0;

length = strlen(string1);

for (i=0; i < strlen(input); i++) {

If (input[i] != input[strlen(input) – i – 1]) {

flag = 1;

Break;

}

}

If (flag == 0) {

// It is a palindrome

} else {

// Not a palindrome

}

### 2. Strings vs char arrays for passwords

**Question**: Which should you use for storing a password; a string or an array of characters?

**Solution**: Always a character array (char[]). Strings are immutable, and any changes made to to them creates a new copy. This means that old copies remain in the memory until garbage collection runs. This makes them vulnerable to other programs that can read that portion of the memory and extract a password. Values of character arrays can be changed without making a copy, hence a char[] is preferred over a string for handling passwords.

### 3. Nth last element in linked list with single traversal

**Question**: Using a single traversal, how do you find the nth last element in a linked list?

**Solution**: This is a tricky question. We don’t know how long the linked list is, nor do we have a pointer to the last element from where we can backtrack n times. However, there’s a very simple solution; use two pointers.

For example, if you want to find out what the third-last element is, move the first pointer with the head, but move the second pointer only when the first one reaches the third element. Once the first pointer reaches the end and can go no further, the second pointer will be at the third-last place.

### 4. Ternary search

**Question**: You have 8 balls that are identical in appearance, but one of them weighs less than the others. How do you find which one it is using a weighing scale? What is the least number of times you must use the weighing scales (worst-case scenario)?

**Solution**: The answer is 2 using **ternary search**. Most people immediately think about binary search: Put 4 balls on each side, and see which weighs less. You eliminate 4 balls this way. Similarly, you weight the remaining balls in pairs of 2 to find out which two balls are lighter. Then it just boils down to two balls that can be weighed against each other to find the lighter ball. That’s 3 steps, which isn’t the best solution.

Using ternary search, you can set aside 2 balls at the start, and weigh 6 balls, 3 on each side. If they weigh equally, that means the lighter ball must be among the two remaining balls. 2 steps required. If the 6 balls don’t weigh equally, then the lighter side containing 3 balls must have the lighter ball. Takes those 3 balls and discard the rest. Weigh two of them against each other again. If they’re equal, then the lighter ball is the third. Otherwise, the lighter ball will be identified on the second step.

### 5. Number swap

**Question**: How to swap two numbers without using a third variable.

**Solution**:

int a = 10;

int b = 20;

a = a + b;

b = a – b;

a = a – b;

b is now equal to 10, and a is equal to 20.

### 6. Recursion

**Question**: Calculate the sum of digits of a number using recursion.

**Solution**: An iterative-based solution to this problem is a simple matter of using a loop and a modulus function. Recursion, however, is a different matter. Here’s the code implementation:

public int digitSum (int number) {

If (number/10 == 0) {

return number;

}

return number%10 + digitSum (number/10);

}

### 7. Factorial using recursion

**Question**: Calculate the factorial of a number using recursion.

**Solution**:

public int factorial (int n) {

If (n > 1) {

return n * factorial(n – 1);

} else {

return 1;

}

}

### 8. Find loops in a linked list

**Question**: How to find out if a linked list has a loop in it?

**Solution**: Use two pointers. Move the first pointer two steps at a time, and the second pointer one step at a time. If there’s a loop, at some point the two pointers will end up pointing at the same location. This method can also be used to find the middle element of a linked list.

### 9. Basics of OOP

**Question**: What are the 4 basic principles of OOP (Object-Oriented Programming)

**Solution**:

- Encapsulation
- Inheritance
- Abstraction
- Polymorphism

### 10. Fibonacci sequence

**Question**: Write a program to calculate the *nth* number in the Fibonacci sequence

**Solution**:

**Iterative**

public int fibonacci (int n) {

int x = 0, y = 1, z = 1;

for (int i = 0; i < n; i++) {

x = y;

y = z;

z = x + y;

}

return x;

}

**Recursive**

public int fibonacci (int n) {

if ((n == 1) || (n == 0)) {

return n;

}

return fibonacci(n – 1) + fibonacci(n – 2);

}

Need help with finding a solution to an interview question? Let us know in the comments section below and we’ll be glad to help you out!