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
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
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
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.
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
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.