Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Friday, July 5, 2024

Top 20 Algorithms Interview Problems for Programmers and Software Engineers

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 task. You got to be lucky to get a call from recruiter or hiring manager and make it to the first round of interview at any stage of your career but it is even more 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 what is expected from you on the coding interview. What type of questions is asked, what topics should you prepare, etc? 

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

Things have also changed a lot in last few years with mandatory coding round and take home test filtering out most of the candidates. That's why I am also updating this article to include recent algorithms questions from coding interviews and how to approach them to impress your interviewer. 

Anyway, coming to Algorithm based questions, these are based upon common searching and sorting algorithms like binary searchgraph algorithms, etc. It’s important that you practice these Algorithm 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.

Having practiced them before 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. That's just some common example and that's why practice matters a lot.





20+ Algorithms Problems and Solutions from Coding Interviews

Anyway, here is some of the frequently asked Searching and Sorting Algorithms problems from Interviews. These are absolutely must know topics for programmers and it doesn't include more advanced algorithms like machine learning algorithms, cryptographic algorithms, encryption algorithms, and advanced String algorithms, which if you guys need, I will write another post about it. 

I have only  included must know algorithms so that most of the people can benefit from this post. 

Now, let's jump into the questions 


1. Can you implement a Binary Search Algorithm? (solution)
It’s easy, binary search is a divide and conquers algorithm, where the problem is divided into sub-problem and those are solved. It’s a search algorithm so 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.

And, here is a nice visual diagram which explains how Binary search algorithms works or find elements in sorted array:

How binary search algorithm work




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 index or true/false depending 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 with an iterative one by using a loop or sometimes a stack. 

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 the middle then you got to move towards the right, or otherwise towards the left.

Here is also a nice diagram which explains how binary search algorithms works:


Algorithms Interview Questions in Java




4. Write a Code to implement Level Order Search in a Binary Tree? (solution)
In level order search you first visit sibling nodes then going down into the next level. You can use a Queue to implement a level order search in a binary tree. 


5. 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 found it's placed in the sorting order. 

This is one of the fundamental algorithms and 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.


6. Difference between a stable and unstable sorting algorithm? (answer)
This one was a tricky concept which 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 merge sort where the former is unstable while the latter is a stable algorithm.


7. What is Depth First Search Algorithm for a binary tree? (solution)
It's another popular searching algorithm which is mainly used in tree 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.  

Top 20  Searching and Sorting Algorithms Interview Questions for Programmers



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. You can further see the solution if you need more help with respect to implementation.


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. 


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.


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.


14. How is a merge sort algorithm implemented? (solution)
Similar to Quicksort, merge sort is also a divide and conquer algorithm i.e. you keep divides the array until you can sort the smallest of the array like an array with one or zero elements. 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 O(nlogn) 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.

Algorithms Coding Problems from Programming Interviews




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. 


16. Write Algorithms to Check if Two String are Anagram (Solution)
An anagram is something where length and character matches but not the order e.g. Army and Mary, both have the same number of characters. One trick is to solve this problem is to sort the character 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.


18. 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. If the concatenated String contains rotation then the given String is a rotation of the former.


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 O(n) sorting algorithms like Radix Sort, Counting Sort, and Bucket Sort.


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.

21. Can we sort in linear time? Which sorting algorithm will you use if you have to sort in O(n) time?
You can use Radix Sort, Counting Sort, and Bucket Sort to sort a group of elements in linear time. 


By the way, the more questions you solve in practice, the better your preparation will be.  So, if you think 50 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 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 coding questions.

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.
Computer Algorithms Interview Questions for Programmers and Software Engineers


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.

This book is considered as the gold standard of algorithms but at the same time it's not easy to go through it and you need to be patient and persistent to get the best out of it. For less brave souls following resources can be more useful.

1 comment :

Anonymous said...

I am not sure but merge sort is not unstable sort. it is stable as mentioned here:
https://www.fullstack.cafe/blog/sorting-algorithms-interview-questions

Post a Comment