Interview Preparation

22 Coding Interview Questions Every Programmer Should Know

Written By Ryan Loftus | May 3, 2022

Coding tests have been the backbone of technical hiring for decades. If you’re a programmer, developer, or engineer on the job market, your ability to demonstrate your skills in a coding interview is critical to landing your next role. 

The key to succeeding in these interviews is preparing well for the styles of problems you might encounter. In this post, we’ll break down four categories of coding interview questions you could face — and 22 sample questions to help you start prepping.

What a Coding Interview Looks Like

Candidate assessment is the process of evaluating whether a developer has the right skill set to succeed in a given role. During the assessment process, various members from the hiring team, including developers, hiring managers, and recruiters, often rely on coding questions — sometimes in the form of a take-home test or live coding challenge — to gauge a developer’s technical proficiency. Coding challenges throughout the interview process gauge the candidates’ problem-solving skills and technical proficiency in various programming languages and frameworks. Tests can also measure a programmer’s ability to navigate a complete application environment, translate log messages, and interchangeably use the command-line, IDE, and browser to interact with the environment. 

It’s important to keep in mind that getting a correct answer to a problem isn’t always enough to pass a coding interview. Interviewers will also be assessing your code quality, technical communication, and the efficiency of the solution.

Examples of coding tests include: 

  • Take-home tests sent during screening
  • Multiple-choice tests
  • Project-based tests 
  • Pair-programming in a virtual IDE

This part of the process is highly variable, with hiring managers able to choose from a number of formats on how to evaluate technical skills. Depending on the assessment platform they use, hiring managers can also change the list of allowed languages the candidate can use to solve the problem. Programmers will need to carefully prepare to ensure they can adapt to a variety of challenges.

Types of Coding Interview Questions

There are a number of different types of coding questions employers can use to evaluate a programmer’s coding skills. The difficulty and composition of these tests will vary based on the experience-level of the role. 

Generally, the more experienced a developer is, the more ambiguous and challenging the questions they will be asked. For example, candidates for entry-level roles might complete coding questions that test execution of predefined problems. In contrast, questions for senior roles can become more open-ended, writing code for problems without predefined solutions. 

The following is a list of the types of coding questions a programmer might encounter during a technical interview.

Algorithms

Algorithm questions are foundational to the technical interview. They test a candidate’s coding skills and ability to solve problems with algorithms in a programming language of their choice.

Sample Question: The Efficient Janitor

The janitor of a high school is extremely efficient. By the end of each day, all of the school’s waste is in plastic bags weighing between 1.01 pounds and 3.00 pounds. All plastic bags are then taken to the trash bins outside. One trip is described as selecting a number of bags which together do not weigh more than 3.00 pounds, dumping them in the outside trash can and returning to the school. Given the number of plastic bags, and the weights of each bag, determine the minimum number of trips the janitor has to make.

This problem tests the candidate’s ability to use two pointers to search for pairs in an assorted array.

More algorithm coding questions:

  • Given an array of integers, find the sum of its elements. For example, if ar = [1, 2, 3], 1 + 2 + 3 = 6. So, return 6.
  • Given a square matrix, calculate the absolute difference between the sums of its diagonals. For example, the square matrix ar = [1, 2, 3, 4, 5, 6 , 7, 8, 9], arranged in a 3×3 grid resembling a phone keypad.
  • You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest. For example, in the array candles = [4, 4, 1, 3], return 2.
  • Given a time in 12-hour AM/PM format, convert it to 24-hour military time. 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock. 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. For example, if s = 12:01:00AM, return 00:01:00.

Data Structures

Data structure is a fundamental concept of computer science and any programming language. Essential for algorithmic design, programmers use data structures for the efficient organization and modification of data.

Sample Question: Binary Search Tree

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function.

A binary search tree is a rooted binary tree data structure with internal nodes that each store a key less than all the keys in its right subtree and greater than all the keys in the node’s left subtree.

This question tests a programmer’s ability to work with data structures to solve problems.

More data structure questions:

  • Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0.
  • You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty.
  • Given a pointer to the head node of a linked list, print each node’s  element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print.
  • A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.

Arrays

An array is a data structure containing elements identified by one or more indexes or keys. Arrays are a fundamental data structure, making them a favorite of interviewers looking to test programmers coding skill. Because the size of an array can’t change, solving this style of question requires finding the optimal way to either rearrange or copy the array into the correct result.

Sample Question: Automated Cutting Machine

An automated cutting machine is used to cut rods into segments. The cutting machine can only hold a rod of minLength or more. A rod is marked with the necessary cuts and their lengths are given as an array in the order they are marked. Determine if it is possible to plan the cuts so the last cut is from a rod at least minLength units long.

This problem tests a programmer’s ability to use arrays, along with loops and greedy algorithms. It requires the programmer to come up with an algorithm to check if an array of given lengths of the rod can be cut from left and right exactly n – 1 time and the remaining size of the rod is at least minLength in a constrained time and space complexity.

More array coding questions:

  • Write a program to check if two arrays are equal.
  • How do you find the smallest and largest numbers in an unsorted array? 
  • There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.
  • A left rotation operation on an array of size n shifts each of the array’s elements unit to the left. Given an integer, d, rotate the array that many steps left and return the result.
  • Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.
  • There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.

Language-Specific Problems

Most coding questions aren’t specific to a particular programming language, allowing a programmer to write code in their language of choice. However, some coding interviews will have questions that test a specific language required by the role.

Sample Question: Enhanced Flexible Grid

Languages: JavaScript, CSS

Create a flexible grid with 1 row and 4 columns. The width of each column is 25% of the window size. This percentage width must be maintained even if the page is resized. Each cell of the grid can contain another 1×4 flexible grid. The border of the grid must be 1px black.

Perform the following operations based on the value of window size:

  • If the window size is less than 720px, then the 1×4 flexible grid becomes a 2×2 grid. That is, the 3rd and 4th columns slide down onto the 2nd row.
  • If the window size is less than 360px, then the 1×4 flexible grid becomes 4×1 grid. Each column slides under the one before it. The 2nd column slides under the 1st, the 3rd slides under the 2nd, and the 4th slides under the 3rd.

This question tests a developer’s ability to write code in JS and CSS and build UI elements. Essential to responsive design, a flexible grid tests a candidate’s ability to perform a task required by a role in front-end development.

More language-specific coding questions:

  • A hacker practices on HackerRank until getting to a rating of O(1) read as (Oh-one). Call the method coder.practice until coder.oh_one? becomes true. Use the until control structure. until is the logical equivalent of while not. This challenge has a one-line answer. (Language: Ruby)
  • Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them. There is not a built-in max function in C. Code that will be reused is often put in a separate function, e.g. int max(x, y) that returns the greater of the two values. Input will contain four integers – a, b, c, d – one on each line. Note: I/O will be automatically handled. (Language: C)
  • This challenge works with a custom-designed markup language HRML. In HRML, each element consists of a starting and ending tag, and there are attributes associated with each tag. Only starting tags can have attributes. We can call an attribute by referencing the tag, followed by a tilde, ‘~’ and the name of the attribute. The tags may also be nested. (Language: C++)
    • The opening tags follow the format: <tag-name attribute1-name = “value1” attribute2-name = “value2” …>.  
    • The closing tags follow the format: </tag-name>
    • The attributes are referenced as: tag1~value, tag1.tag2~name
    • Given the source code in HRML format consisting of N lines, answer Q queries. For each query, print the value of the attribute specified. Print “Not Found!” if the attribute does not exist.
  • Given an integer, n, perform the following conditional actions:
    • If n is odd, print Weird
    • If n is even and in the inclusive range of 2 to 5, print Not Weird
    • If n is even and in the inclusive range of 6 to 20, print Weird
    • If n is even and greater than 20, print Not Weird
    • Input format: a single line containing a positive integer n. Constraints: 1 <= n <= 100.
    • (Language: Java)

Resources to Prepare for Coding Interviews

HackerRank for Developers

Free Developer Skills Platform

15 Common Problem-Solving Interview Questions

7 System Design Interview Questions Every Software Engineer Should Know

Software Engineering Questions to Know in 2022

11 Artificial Intelligence Interview Questions You Should Know