56 Popular Programming Interview Questions Every Developer Should Study
You learned how to write code and possibly have a computer science degree, which means you’re on the right track to getting a developer job. While a programming interview might seem scary at first, a little preparation can go a long way toward easing your anxiety. Interviews for technical programming jobs still employ a wide variety of testing practices, but programming job applicants will most certainly be expected to take a verbal or written test designed to assess their technical skills.
You will be better prepared for your next programming interview if you review the questions interviewers are likely to ask. To help you, we have compiled a list of the most common interview questions along with example answers. Reviewing them will refresh your knowledge and give you confidence on the day of your interview.
Featured courses in Web Development
Technical questions
In some ways, the technical part of the interview is the easiest to prepare for. Most of the questions will have straightforward right or wrong answers. Still, there is an art to programming, so be ready for problems with more than one correct answer. Read the question and try to understand what it is really asking. Maybe one solution is more efficient, produces more readable code, or is more stylistic as opposed to technical.
Let’s look at some specific programming concepts and the possible questions you could encounter for each one.
Programming languages
All programming languages have their benefits and drawbacks, as they work for a specific use or a range of applications. A development shop that uses multiple languages will try to determine what you know about languages in general, and more specifically, how you would choose a tool for a task.
1. What is the difference between a high and low-level language?
We use high-level languages for process flow and business logic, unlike low-level languages that provide tools for manipulating the operating system and hardware. Low-level languages are often harder to learn and difficult to read. For a system with no low-level requirements, choosing a low-level language will result in an overly complicated program that is difficult to maintain.
2. What is the difference between an assembler, a compiler, and an interpreter?
An assembler converts assembly code into machine language. It is the lowest level of programming, so software engineers use it with programs that interact with the machine. Examples could include a device driver or a low-level embedded system.
Compilers parse source code written in a high-level language and translate it into machine-level object code. The object code is a module that can execute after the compilation process has completed processing all files.
Interpreters read source code written in a high-level language and execute the source code as it reads it line-by-line.
3. What is an IDE?
An Integrated Development Environment (IDE) is a software tool used by developers to write programs. Depending on the IDE and language, there will be facilities to compile or run an interpreter to prepare and execute the code. IDEs will include debugging tools to debug programs.
4. What is a runtime error?
A runtime error is an error that happens when an application is running. We call it this to distinguish it from other types of errors, like syntax or compile-time errors — these are usually detectable before an application runs. Runtime errors are generally handled by the application, giving the user an option to ignore the error and continue working. Other times, they may cause the program to crash.
5. What is a syntax error?
A syntax error happens when the code does not follow the grammatical rules of the programming language. If the language uses a compiler, it will occur at compile time. If it is an interpreted language, it will be detected when the application executes.
6. What are reserved words?
A reserved word is a word that you cannot use as an identifier in your code. These differ from keywords that actually have a function in the programming language. Reserved words may not or may not have a function in the language.
Data structures
Every program contains two elements: data and processes or algorithms that use and manipulate the data. It is important to understand how to structure data to manage the program’s size and provide efficient access to the data. The following questions will test your knowledge of data structures.
Constants and variables
7. What is the difference between a constant and a variable?
A constant is a data element with a value that cannot change. It is initialized with a type and value and remains that way while the program executes. A variable is a data element initialized with a type and a value that can change during the program’s execution.
8. What is the difference between a local and a global variable?
Global variables are declared outside of a function and can be accessed or manipulated in any function. Local variables are declared within a function and are only accessible while that function is executing.
9. What is a static variable? What is a dynamic variable? What is the difference between them?
Stacks and arrays are examples of static variables. We manage memory for static variables, and there is a cap on how much memory we can use. An example of a dynamic variable is a linked list. Programmers manage the space by allocating and deallocating memory as required.
10. What does it mean when a variable is in or out of scope?
Scope refers to the visibility and access of a variable. A variable is in scope when the program can see or use it.
11. What is the difference between an int, char, float, and double variable?
An int is a variable that can store an integer or whole number. A float is a number with a decimal point. We call it a float because there is no fixed number of digits before or after the decimal point. A double is the same as a float, except it can store larger numbers.
A char is a variable that can store a single letter, number, or symbol character.
12. What is the difference between null and void?
We use null and void with variables. Null refers to the value and indicates that the variable is not ready for use because it does not have a valid value. Void refers to the type, and a void type is unknown.
Strings
Strings are pretty straightforward. Most questions will test your knowledge of the language and libraries. Know how to process strings with and without library routines. String-related questions you could encounter include:
13. Find duplicate characters in a string.
Here is a method that will find the duplicate characters in a string in Java:
public class DuplicateStr {
public static void main(String argu[]) {
String str = "Java School";
int cnt = 0;
char[] inp = str.toCharArray();
System.out.println("The Duplicate Characters are:");
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (inp[i] == inp[j]) {
System.out.println(inp[j]);
cnt++;
break;
}
}
}
}
}
14. How do you convert a string to an integer?
int i=Integer.parseInt("200");
15. How would you convert an integer to a string?
int i=10;
String s=String.valueOf(i);
16. How do you split a string based on a character?
//Splitting a string on a dash
String s = “this-haas-dashes”;
String parts[] = s.split(“-”);
17. What does it mean if a string is immutable?
If a string is immutable, it means you cannot change it after creation. Instead, you will have to create a new string.
Arrays
An array is a data structure that contains a sequence of elements. The size is determined at the time of declaration and cannot change. In most cases, the elements are all the same type — like a set of strings or integers. The following are common array questions:
18. Arrays are declared empty with a size or with a set of values. How do you add or delete elements of an array declared with values?
Here are a couple of ways to add an element to an array in Java:
- You can use a new array with one more element. Then you can iterate the original array, copying elements to the new array and adding the new element at the end.
- You can use an ArrayList as an intermediate structure by first using the asList() method of the array, then adding an element to the ArrayList, and finally using the toArray() method of the ArrayList.
There are also a few ways to delete an element:
- You can use a new array with one less element, copying all the elements to it except the one you want to delete.
- You can convert the array to a stream, add the element to the stream, and then convert the stream back to an array.
- You can convert the array to an ArrayList, remove the element, and then convert the ArrayList back to an array.
19. Given an array with values, how do you find duplicates or pairs?
There are a few ways to find duplicates in a Java array:
- You can loop over the array and compare each element to every other element. This requires an inner loop and an outer loop and is not very efficient.
- You can use a HashSet to check for duplicate values. This data structure will not allow duplicates. When you try to add a duplicate value, the add() method will return false. By looping the array and attempting to add the elements to the hash set, you will find all the duplicate values.
- You can insert the elements of the array into a HashTable by looping the array and adding the count to the table.
20. If an array initializes with an unsorted series of numbers, how would you find the largest or smallest value?
Here is a Java class to find the largest and smallest element of an array:
class MinMax
{
static int arr[] = {108, 865, 10, 84, 1208};
// Method to find the largest element
static int largest()
{
int i;
// Initialize largest element variable
int max = arr[0];
// compare all elements to current max
for (i = 1; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
// Method to find the smallest element
static int smallest()
{
int i;
// Initialize smallest element variable
int min = arr[0];
// compare all elements to current min
for (i = 1; i < arr.length; i++)
if (arr[i] > min)
min = arr[i];
return min;
}
}
21. How do you sort an array by ascending or descending value?
Calling the sort() method of a Java Arrays class will sort the array in ascending order by default.
import java.util.Arrays;
int[] intArray = {52, 45, 32, 64, 12, 87, 78, 98, 23, 7};
Arrays.sort(intArray);
To sort the array in ascending order, you must call the sort() method with a second argument from the Java Collections class.
import java.util.Arrays;
import java.util.Collections;
int[] intArray = {52, 45, 32, 64, 12, 87, 78, 98, 23, 7};
Arrays.sort(intArray, Collections.reverseOrder());
22. How do you reverse the values in an array? Do you need a second array, or can it be done using the existing array?
There are a few ways to reverse the elements in a Java array:
- You could create a new array and loop the old array, adding each element to the back of the new array as you go.
- You could write a function to swap each element from the front to the back of the array. This method doesn’t require a second array.
- The third method you could use is converting the array to a list, using the Java Collection class reverse() method, and then converting the list back to an array.
23. How do you sum the values of an array?
Create a variable to hold the sum and loop the array, adding each element to the sum.
24. How do you find duplicate numbers in an array if it contains multiple duplicates?
Since multiple duplicates would involve keeping a count, you could use a HashTable to hold these values with their counts and then loop the array to collect the counts.
25. How do you find the largest and smallest number in an unsorted integer array?
A simple way to do this would be to call the sort() method of the array. Then the largest element will be at the 0 index of the array, and the smallest element will be at the length-1 index of the array.
26. Hashing is the process of converting a given key or value to another representative value. A good hashing algorithm is one way — you cannot decode the hashed value to uncover the original value. Can you describe a situation where you would use hashing?
One of the most common places to use hashing is for storing passwords for login procedures. Keeping the password in plain text in a database is a security risk. Storing a hashed value of the password will allow for comparing it to the hashed value of an entered password for securely authenticating users.
Linked lists
Linked lists are dynamic data structures used in stacks, queues, and other abstract data types. You could face a number of questions on linked lists in an interview, such as:
27. How do you instantiate a new linked list?
List<Double> list = new LinkedList<Double>(Arrays.asList(1.2,1.3,3.2));
28. Why is it faster to add or delete a node in an unsorted linked list versus a sorted linked list?
Inserting items in a sorted linked list involve more operations than inserting them in an unsorted list.
29. How does an algorithm traverse a linked list forward?
Here is how a recursive algorithm would transverse a linked list forward:
- Check if you are at the end of the list and exit if so
- Do the operations that you need to do on the current node
- Make the current node the previous node and the next node the current node
- Repeat step 1
30. How to remove duplicates from a sorted linked list?
Traverse the list from the start node, comparing each node with the next node. If the values of these nodes are the same, then store a pointer of the node after the next node, delete the next node, set the pointer you just stored as the current node, and repeat this process until the current node is null.
31. How would you find the length of a singly linked list?
There are a couple of ways to find the length of a singly linked list in Java:
- You can iterate the list adding to a count variable as you go and exiting when the current node is null.
- You can use a recursive solution like the following:
public int length(Node current){
if(current == null){ //base case
return 0;
}
return 1+length(current.next());
}
32. Arrays and linked lists are both linear data structures. What are some of the differences between the two data structures, and why would you choose one over the other?
Linked lists are a dynamic size, and elements can be easily inserted and removed, unlike arrays. But arrays allow random access and have better cache locality. You would use a linked list when you don’t know how many elements will be added and use an array when you need to do a binary search on the elements.
Stacks and queues
Stacks and queues are both non-primitive data structures. They are both ordered lists of elements where operations add and remove elements from the list. Typical questions regarding these include:
33. What are the two basic operations of a stack?
The top basic operations of a stack are push, for adding an element to the collection, and pop, for removing the most recent element.
34. What are the two basic operations of a queue?
The two basic operations of a queue are enqueueing, or inserting an element, and dequeuing, or deleting an element.
35. Where are queues commonly used?
You commonly use queues for the following tasks:
- To check if delimiters are balanced in an IDE or compiler
- To reverse strings
- To traverse nodes of a binary tree
- To search the vertices of a graph
36. Where are queues commonly used?
You commonly use queues for the following tasks:
- To undo mechanisms in text editors
- To convert infix to postfix
- To schedule algorithms
- For recursive function execution
37. What is the primary difference between a stack and a queue?
A stack uses the LIFO (last in first out) method to add and remove elements while a queue uses the FIFO (first in first out) method to add and remove elements.
Binary trees
Binary trees are not a single data structure. They are a family of data structures with different performance characteristics.
Low-level technical applications use binary trees. Some examples include:
- 3D video games
- Router-tables in high bandwidth routers
- Heaps
- Priority queues
- Quality-of-service (QOS) routers
- A* (path-finding algorithms used in AI applications like robotics and video games)
- Treaps (randomized data structures used in wireless networking and memory allocation)
Binary tree questions might include:
38. What is the maximum depth of a binary tree?
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
39. What is InOrder traversal of a binary tree?
An InOrder traversal of a binary tree traverses the left subtree, visits the root, and then traverses the right subtree.
40. What is PostOrder traversal of a binary tree?
An PostOrder traversal of a binary tree traverses the left subtree, traverses the right subtree, and then visits the root.
41. How do you traverse a given binary tree with recursion?
You can use recursion for InOrder, PostOrder, or PreOrder traversal. A recursive method will call itself to access the next node in the tree.
42. How do you traverse a binary tree without recursion?
Here is an algorithm to traverse a binary tree without recursion:
- Create an empty stack
- Initialize the current node as root
- Push the current node to the stack and set the current node to the left node until the current node is null
- If the current node is null traversing left, then:
- Pop an item from the stack
- Perform the operations you need to on the item you popped
- Set the current node to the right node of the popped item
- Go to Step 3 if the current node is not null
- If the current node is null, the process is done
43. The binary tree is said to be balanced if the difference between the height of the left side and the right side is no more than one. How do you check if the binary tree is balanced?
To check if a binary tree is height-balanced, first find the height of the left and right subtrees. Then return true if the difference between heights is not more than one and left and right subtrees are balanced, otherwise return false.
Algorithms
Data sorting
Data is not always stored sorted. This is normally done for improved insert and delete performance, but there will be times you need to sort an unsorted list. That’s why it’s important to know how to write an algorithm to sort the data quickly.
Let’s look at some of the better known sorting algorithms and questions you may encounter related to them.
44. What is the difference between a comparison and a non-comparison sort?
A comparison sort sorts items by comparing values against each other. A non-comparison sort sorts items by the internal character value of the items.
45. An insertion sort is a simple sorting method that works the same way you would sort a hand of playing cards. Each element is inspected and sorted in the front of the array until it has sorted all the elements. How is this different from the selection sort?
A selection sort sorts by selecting the smallest item from the array of items and exchanging it with the item at the correct location.
46. We explicitly define some sorts to be efficient, such as the Merge, Heapsort, Quicksort, and Shellsort. Can you explain one of the sorting techniques and what makes it efficient?
Quicksort is a divide-and-conquer algorithm. It selects a pivot element from the array of items and partitions the other elements in two sub-arrays, which sort recursively. This can be done in place and use small amounts of memory.
47. The Bubble sort is one of the simplest sorts to implement. The algorithm parses the data from start to end, repeatedly swapping adjacent elements if they are not in order. This parsing executes multiple times until there are no elements to swap or data to sort. How does the Comb sort improve on this?
A Comb sort improves on a Bubble sort by eliminating “turtles,” which are small numbers at the end of the sorted list. A Comb sort does this by comparing values that are farther apart.
48. Distributed sorts use multiple intermediate data structures during the sorting algorithm, which are individually sorted and then combined at the end. Examples of these sorting routines include Counting, Bucket, and Radix. Can you describe one of these algorithms and when you would use it?
A Counting sort is an algorithm that works by counting the number of objects that have distinct key values and then using arithmetic to determine the order of each key value in the output. You would use it to sort integers.
Recursion and iteration
Algorithms use loops and recursion to parse lists of data and repeatedly perform a set of instructions for each list element. You could be asked about the following as it relates to them:
49. Loops are control structures used to repeat a given section of code until it meets a particular condition. What is the difference between the different loop types: For, Foreach, While, Do While?
- For – This loop iterates through a section of code a set number of times.
- Foreach – This loop is for looping through collections.
- While – This loop iterates through a section of code while a condition is true.
- Do while – This loop is the same as the while loop but with a single loop that always executes at the start.
50. What is the difference between loops with the termination condition at the top, like the While statement, versus at the end, like the Do statement?
Both of these loops function similarly, but since the Do checks the termination condition at the end, the code within the loop is guaranteed to execute at least once.
51. How does the Break statement function in a loop?
The Break statement in a loop stops the loop and continues program execution at the statement directly after the loop.
52. What is recursion?
Recursion is a process by which a method calls itself either directly or indirectly. Using recursion, you can quickly solve complex problems.
General problem-solving questions
Some questions will be process-related. The list below presents a few simple programming job interview questions representing what could be on a test to exercise your programming logic skills.
53. What are the Fibonacci numbers, and how or when are they used?
The Fibonacci numbers, or Fibonacci sequence, is a series of numbers where each number is the sum of the two preceding it. Here is Fibonacci sequence:
0,1,1,2,3,5,8,13,21,34,55,89,144
Fibonacci numbers are useful for:
- Pseudorandom number generation
- In a polyphase version of the merge sort algorithm
- For technical market analysis
54. What are random number generators?
A random number generator is a method that can generate a sequence of numbers that is impossible to predict better than by random chance.
55. How would you find all the prime factors of a given number?
Here is a method to do that in Java:
import java.util.Scanner;
public class PrimeFactors {
public static void main(String args[]){
int number;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
number = sc.nextInt();
for(int i = 2; i< number; i++) {
while(number%i == 0) {
System.out.println(i+" ");
number = number/i;
}
}
if(number >2) {
System.out.println(number);
}
}
}
Top courses in Development
56. What is the frequency of a given word in a paragraph or article?
To solve this problem, you would first have to split the string on spaces. This will give you an array of words in the string. Then you would loop over this array, collecting the words and incrementing their count in a Map.
Business knowledge
For recent graduates, the hiring manager will likely select you for your technical knowledge, a belief in your teamwork, and a capacity to learn. Nonetheless, it is always a good idea to research the company and department you are interviewing with. If you are interviewing for the capital markets trading desk at an investment bank, you should know about capital markets, the instruments traded, and the trade types. You don’t have to know this in great detail, but enough that allows you to relate to the topic. When the hiring manager discusses what the department and team do, a little background information will help you follow the conversation and ask good questions.
Presentation skills
Estimates show that more than 60% of hiring managers use video for interviewing. In 2021 and beyond, the use of video will only increase and extend to remote working teams.
The interview process will likely include your presentation preparedness and skills. If it’s a remote interview, your video communication skills will be on display.
A few ways to be a standout candidate include:
- Get a good quality camera and microphone.
- Set up in front of a neutral background.
- Have good natural lighting that minimizes shadows.
- Find a quiet spot where you will not be interrupted.
- Practice looking into the camera. If you are using your computer, look at the camera, not the middle of the screen.
- Dress as you would for an in-person meeting — top to bottom with shoes!
- Practice with a friend using different technologies. Zoom, Skype, Microsoft Teams, and WebEx are common video platforms used today.
- Have a backup. Set up the meeting with your cell phone connection or a hotspot. If your internet connection goes down, rejoin with your backup. You will most likely have less than a minute before the host leaves the meeting.
Conclusion
As you prepare for your interviews, remember that while you are interviewing for a programmer job, managers will look for well-rounded team members.
Each hiring manager will look for a balance. Some managers find it easier to onboard candidates with common sense or practical intelligence, while others prioritize technical competence. A manager that finds it easy to improve technical skills will emphasize candidates with soft skills. Alternatively, a manager that finds it easier to teach about the business and soft skills will emphasize technical skills. It is important to present yourself as a candidate that is open and capable of learning because, ultimately, the hiring manager is responsible for managing a team and mentoring team members to grow their skills.
Recommended Articles
Top courses in Web Development
Web Development students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.