Coding Interview Preparation [Core Java]

In this post, I shall be sharing my notes of coding interview preparation for Java Developers. If you are about to give an interview for a Java developer in any of the MNC's like TCS, Infosys, Capgemini, Cognizant, Wipro, HCL, etc. then you have landed in the right place. Suppose you are giving HackerRank or HackerEarth or any other coding test and you have to write the code for the question that requires iteration of the HashMap. Believe me, you will not be able to crack coding interview questions just because you don't know how to iterate a HashMap. Let's dive deep into the questions:

Read Also: Best Books for Coding Interview in Java

Note: Before looking at the answer, I suggest writing the code in your favorite IDE (Eclipse/IntelliJ) first.

Coding Interview Preparation with Code Snippets

1. Sort Objects using Comparator


This question is very important. This question has been asked to me in many coding interviews. Please go through it once before appearing for the coding interview.

Code Snippet



sort objects using comparator


Developer class java


2. Sort HashMap by Values


I bet a few Java developers can sort HashMap by Values in the first attempt. If you find it hard to solve, then I will suggest to remember the code using Java 8.

Code Snippet



Sort HashMap by Values

3. Sort HashMap by Values in Descending Order


What if you need to sort the HashMap by Values in descending order. Just go through the below code snippet.

Code Snippet

Sort HashMap Values Descending Order

4. Find Permutation and Combination of a String in Java


It is difficult to code finding permutation or combination of a given String on the spot at the interview. I will suggest just go through the below code snippet and remember it as they are similar.

Code Snippet

Note: Code for finding Permutation or Combination is almost similar as shown below.


Permutation and Combination in Java

5. Iterate HashMap


If you have more than 2 years of experience then you must know the code how to iterate the HashMap in Java. There are many ways to iterate through the HashMap. Below I shared the one which is easy to remember using Map.Entry class.

Code Snippet



Iterate HashMap in Java


That's all for today, please mention in the comments in case you know any other java code that is frequently asked and hard to solve at the first attempt.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry