Top 20 Algorithms Coding Problems for Programming Interviews with Solutions

Preparing for coding interviews? Here are 20+ algorithms problems you can practice. This list included questions on essential searching and sorting algorithms like binary search, quick sort, counting sort, etc.

javinpaul
Javarevisited
14 min readMay 11, 2020

--

20+ Algorithms Coding Problems for Programming and Technical Interviews

Hello All, If you are preparing for Programming job interviews or looking for a new job, then you know that it’s not an easy process. You got to be lucky to get the call and make it to the first round of interviews, not just when you are a beginner but at any stage of your career.

But, Yes, it is the most difficult at the beginner level when you are searching for your first job.

That’s why you can’t just take your chance lightly. You got to be prepared to grab that chance and for that, you must know that is expected from you on the interview. What is asked, what topics should you prepare, etc?

I have blogged a lot about what you can find helpful articles in this blog but to recap let me tell you that apart from data structure questions, System Design Questions, and Programming language-specific questions like Java, C++, or Scala, most of the programming job interviews also ask algorithm based questions.

These are based upon common searching and sorting algorithms like String algorithms, binary search, graph algorithms, etc.

It’s important that you practice these Algorithms based questions because even though they seem obvious and easy, sometimes they become tricky to solve in the actual interview, especially if you have never coded them by yourself.

Practising these problems before interview not only makes you familiar with them but also gives you more confidence in explaining the solution to the interviewer, which plays a very important role in your selection.

It also makes you ready for any twisted questions, and alternative problems like Interviewers often like to ask you to solve a particular coding problem using Recursion or Iteration.

Sometimes, if you use a data structure like the one I have used in finding duplicate characters on String, they will ask you to solve that problem without using the Set data structure. Those are just some common examples, and that’s why practice matters a lot.

Btw, if you are a complete beginner in the world of Data Structure and Algorithms, then I suggest you first go through a comprehensive Algorithm course like Data Structures and Algorithms: Deep Dive Using Java on Udemy which will not only teach you basic data structure and algorithms but also how to use them on the real world and how to solve coding problems using them.

On the other hand, if you like to read books or prefer books over online courses, then you must read a comprehensive book like Introduction to Algorithms by Thomas H. Cormen to get an understanding of common Computer Science Algorithms like Searching, Sorting, Cryptography, Graph Algorithms and some common ones like Fourier Transform.

20+ Searching and Sorting Algorithms Questions from Coding Interviews

Anyway, here are some of the frequently asked Searching and Sorting Algorithms questions from Interviews. I have linked the solution, but you should try to solve the problem before looking at the solution.

The purpose of this article is that you should know how to solve these problems on your own, but, yes, if you got stuck and want to compare your solution, you can see the solution.

Searching Algorithms based Coding Problems

Let’s first start with basic search algorithms like linear search, binary search, level order search, and depth-first search algorithms.

1. Can you implement a Binary Search Algorithm? (solution)

It’s easy, binary search is a divide and conquer algorithm, where the problem is divided into sub-problem, and those are solved. It’s a search algorithm, which means it is used to find things like a number in an integer array or an item in a catalog.

The easiest way to implement a binary search algorithm is by using Recursion, which is what the solution link contains, but you should try it yourself before seeing the solution.

One of the worth noting is that the input must be sorted, I mean you can only implement binary search in a sorted array.

2. Write a program to implement a Linear Search Algorithm? (solution)

It is even easier than binary search, all you need to do is go through all elements in the array using a for loop or recursive method and compare each element with the one you want to search. When an element matches, you either return the index or depend upon your requirement.

For example, if you are writing a contains() method you can return true or false to indicate whether an element is present in the array or not. Since you need to scan the whole array to find the element, the time complexity of this algorithm is O(n).

3. Can you implement a Binary search Algorithm without recursion? (solution)

You might know that you can replace a recursive algorithm to an iterative one by using a loop and sometimes using a Stack data structure. For binary search also you can do this; just divide the array and compare the middle element until you find the target element or there is no more element into an array.

If the target element is greater than middle than you got to move towards the right, or otherwise towards left.

Btw, if you have trouble understanding recursive algorithms or converting a recursive one to an iterative one, then I suggest you go through a good online course like Algorithms and Data Structures — Part 1 and Part 2 in Pluralsight to learn fundamentals better.

These courses will also teach you about how to calculate time and space complexity, which is very important from both the Coding Interview perspective as well as improving the performance of an Algorithm.

By the way, you would need a Pluralsight membership to join this course which costs around $29 per month or $299 per year (14% discount). I highly recommend this subscription to all programmers as it provides instant access to more than 7000+ online courses to learn any tech skill.

Alternatively, you can also use their 10-day-free-pass to watch this course for FREE.

searching algorithm interview questions

4. Write Code to implement Level Order Search in a Binary Tree? (solution)

In level order search, you first visit sibling nodes then go down into the next level. You can use a Queue to implement a level order search in a binary tree.

And, if you are really serious about doing well, you can also check this list of courses to crack your programming job interviews

5. What is Depth First Search Algorithm for a binary tree? (solution)

It’s another popular searching algorithm that is mainly used in trees and graphs. This algorithm first visits nodes in depth before searching at the same level; that’s why the name Depth-first search algorithm.

It’s tricky to implement, but you can use a Stack to implement DFS or Depth-first search algorithm. If you need more information on this topic, I suggest you check the Grokking Algorithms book by Aditya Bhargava; his explanation is probably the best explanation of this topic

tree algorithms interview questions

Sorting Algorithms based Coding Problems

Now we have seen some coding problems based upon search algorithms, let’s dive into coding problems based on sorting algorithms:

6. Implement the Bubble sort Algorithm? (solution)

Isn’t this was the first sorting algorithm you learn? Well, I did, and that’s why I remember that bubble sort is about comparing each number with every other number in an array so that after each pass, the largest or smallest element bubble up to the top.

I mean, the number has been found it’s placed in the sorting order. This is one of the fundamental sorting algorithms, and most of us started learning about sorting using this algorithm.

The time complexity of this is O(n ^2) which makes it unusable for a large set of numbers but it does well for a small set of numbers. If you want to learn more, you can check any of these free data structure and algorithms courses on freeCodeCamp

7. Difference between a stable and unstable sorting algorithm? (answer)

This one was a tricky concept that I didn’t know until long ago. I haven’t come across any practical use case of this one yet, but just knowing the concept is Ok from the interview perspective.

In a stable sorting algorithm, the order of the same element remains the same even after sorting, but during the unstable sorting algorithm, these changes.

A good example is a quicksort and mergesort, where the former is unstable while latter is a stable algorithm.

8. How is an iterative quicksort algorithm implemented? (solution)

Obviously without recursion:-). If you remember, I have told you before that you can use a Stack to convert a recursive algorithm into an iterative one and that’s what you can do as well to implement the Quicksort algorithm without recursion.

Btw, if you have trouble calculating and understanding the time and space complexity of algorithms, then you should see a course like Data Structures & Algorithms — Interview to understand them better before going for an interview.

sorting algorithms interview questions

9. How do you implement a counting sort algorithm? (solution)

Just like we have done with other O(n) sorting algorithms like Radix sort and Bucket sort.

If you don’t know, Counting sort is another integer sorting algorithm for sorting a collection of objects according to keys that are small integers.

It has O(n) time complexity, which makes it faster than the likes of Quicksort and Mergesort for a particular set of input. See the solution for more details.

10. How do you swap two numbers without using the third variable? (solution)

Another tricky question which is easy if you know the trick :-) Well you can swap two numbers without using a temporary or third variable if you can store the sum of numbers in one number and then minus the sum with another number something like

a = 3;
b = 5;

a = a + b; //8
b = a — b; // 3
a = a — b; //5

now you have a = 5 and b = 3, so numbers are swapped without using a third or temp variable.

11. How is a radix sort algorithm implemented? (solution)

This is another integer sorting algorithm with O(n) time complexity. As per Wikipedia, Radix sort is a non-comparative sorting algorithm that sorts data with integer keys by grouping keys by the individual digits, which share the same significant position and value.

You can further see Algorithms, Part I, and Part II by Robert Sedgewick on Coursera to learn more about these or liner sorting algorithms. The course is free for learning and exploring, but you need to pay if you also want a certification.

20+ Algorithms Coding Problems for Programming and Technical Interviews

And, if you find Coursera courses useful, which they are because they are created by reputed companies like Google, IBM, Amazon, and the best universities around the world, I suggest you join the Coursera Plus, a subscription plan from Coursera

This single subscription gives you unlimited access to their most popular courses, specialization, professional certificate, and guided projects. It cost around $399/year but its completely worth of your money as you get unlimited certificates.

12. How do you implement an insertion sort algorithm? (solution)

Have you ever arranged the deck of cards, or maybe shirts in your cupboard? What is common between those two things? Well, you put the next card or shirt into their proper position, or, should I say you insert the next element in its proper position. That’s the insertion sort for you.

Miscellaneous Algorithms based Problems from Tech interviews

Now, let’s see a few more coding problems which are based upon different algorithms.

13. Write the Algorithm to check if two rectangles overlap with each other? (solution)

This is a tricky Algorithm question, but if you have to listen to your teacher in your 2D Maths class, then you can solve this problem. There is another trick, check for all the conditions when rectangles will not overlap, and if any condition is false, it means both rectangles are overlapping with each other. For example, if the upper side of one rectangle is below the lower side of other rectangles, then they won’t overlap as they are vertically aligned.

Btw, if you find this question tough to solve, I suggest you take a look at the Grokking the Coding Interview: Patterns for Coding Questions course on Educative, an interactive portal for coding interviews to learn some 16 useful coding patterns like Sliding Window, Two Pointers, Fast and Slow Pointers, Merge Intervals, Cyclic Sort, and Top K elements that can help you to solve zones of frequently asked coding problems.

sliding window interview questions

14. How is a merge sort algorithm implemented? (solution)

Similar to Quicksort, merge sort is also a divide and conquers algorithm, which means you keep the problem until you can sort the smallest of them.

For example, to sort an array of numbers, you divide the array into smaller parts until you know how to sort them like an array with one or zero elements is already sorted. Once you sort small arrays, you merge them to get the final result.

The only difference between Quicksort and Mergesort is that mergesort is stable while Quicksort is not-stable. This means equal elements retain their spot before and after sorting.

Another worth noting difference is that even though both have average time, it’s better to use quicksort than mergesort because Quicksort takes less time for the same number of input, the constant factor is less in Quicksort than merge sort.

merge sort algorithms interview questions

15. How do you implement a bucket sort algorithm? (solution)

The Bucket sort is another awesome algorithm that can sort an array without even comparing elements. It’s known as a non-comparison sorting algorithm and can give O(n) performance for the selected input.

If you don’t know about the non-comparison-based sorting Algorithm, please see Introduction to Algorithms book.

best books to learn algorithms

16. Write Algorithms to Check if Two String are Anagram (Solution)

An anagram is something where length and character match but not the order like Army and Mary, both have the same number of characters.

One trick is to solve this problem is to sort the character array and check if they are the same or not.

17. Implement the QuickSort Algorithm in your Favorite Programing language? (solution)

This one is a very easy sorting algorithm, but only if you have practiced, if not, then you may lose your way. Remember, Quicksort is a divide and conquer algorithm, which means you keep dividing array, also known as partitioning. Then you solve the problem at the smallest level, also known as a base case like when your array contains just one or zero elements.

19. Difference between Comparison and Non-Comparison Sorting Algorithms? (answer)

As the name suggests, in comparison-based sorting algorithms, you must compare elements to sort like quicksort, but in non-comparison-based sorting algorithms like Counting sort, you can sort elements without comparing them. Surprised?

Well yes, then I suggest you check out this course to learn more about sorting algorithms like Radix Sort, Counting Sort, and Bucket Sort. You can further see Data Structures and Algorithms: Deep Dive if you want to learn more about these sorting algorithms.

array algorithms interview questions

19. How to check if two String is rotations of each other? (solution)

There is a simple trick to solve this problem, just concatenate the string with itself and check if the rotation exists there. You can do that by using indexOf or substring method. If the concatenated String contains rotation, then the given String is a rotation of former.

20. Implement Sieve of Eratosthenes Algorithms for Prime Number? (solution)

This is one of the tough algorithms to implement, especially if you don’t remember it :-) Sometimes, the interviewer gives you the explanation, but other times, you need to remember it.

I hope these 20 questions should be enough to get you going on your preparation for Algorithms for Coding interviews. If you need more such coding questions, you can take help from books like Cracking The Code Interview by Gayle Laakmann McDowell which contains 189+ Programming questions and solutions. A good book to prepare for programming job interviews in a short time.

best book for coding interviews

By the way, the more questions you solve in practice, the better your preparation will be. So, if you think this list of questions is not enough and you need more, then check out these additional 50 programming questions for telephone interviews and these books and courses for more thorough preparation.

Now You’re Ready for the Coding Interview

These are some of the most common questions outside of data structure and algorithms that help you to do really well in your interview.

I have also shared a lot of these questions on my blog, so if you are really interested, you can always go there and search for them.

These common coding, data structure, and algorithms questions are the ones you need to know to successfully interview with any company, big or small, for any level of programming job.

If you are looking for a programming or software development job, you can start your preparation with this list of courses to learn patterns, tips, and tricks to solve coding problems

Resources to Prepare for Tech Interviews

  1. Grokking the Coding Interview: Patterns for Coding Questions
  2. Data Structures and Algorithms: Deep Dive Using Java
  3. From 0 to 1: Data Structures and Algorithms in Java
  4. Data Structure and Algorithms Analysis — Job Interview
  5. Cracking The Code Interview,
  6. Grokking the System Design Interview

This list provides good topics to prepare and also helps assess your preparation to find out your areas of strength and weakness.

Good knowledge of data structure and algorithms is important for success in coding interviews, and that’s where you should focus most of your attention.

Closing Notes

Thanks, You made it to the end of the article … Good luck with your programming interview! It’s certainly not going to be easy, but by following these searching and sorting algorithm questions, you are one step closer than others.

If you like this article, then please share it with your friends and colleagues, and don’t forget to follow javarevisited on Twitter and javinpaul and javinpaul here on Medium as well!

P. S. — If you are preparing Tech Interviews then I also suggest you go through Grokking the Coding Interview: Patterns for Coding Questions to learn 16 useful coding patterns like Sliding Window, Two Pointers, Fast and Slow Pointers, Merge Intervals, Cyclic Sort, and Top K elements that can help you to solve dozens of frequently asked coding problems.

Other Data Structure and Algorithms Articles from Medium

--

--

javinpaul
Javarevisited

I am Java programmer, blogger, working on Java, J2EE, UNIX, FIX Protocol. I share Java tips on http://javarevisited.blogspot.com and http://java67.com