Article start
Java Backend Interview Prep
3. OOP & DSA Revision

14. Sorting, Maps, Strings, and Character Checks

Practice common interview snippets for sorting, maps, equality, and digit checks.

May 29, 2026·23

Sorting

Use Arrays.sort(array) for arrays and Collections.sort(list) or list.sort(comparator) for lists.

For custom objects, implement Comparator<T> or pass a lambda comparator:

students.sort(Comparator.comparing(Student::getRollNo));

Maps

getOrDefault is common for frequency counting:

count.put(word, count.getOrDefault(word, 0) + 1);

Sort map entries by value by copying entrySet() into a list and sorting with Map.Entry.comparingByValue().

Strings and Characters

Use .equals() for String content comparison. == compares references for objects.

Use Character.isDigit(c) and Character.isLetter(c) for character classification.

Interview Scenario Practice

Scenario 1: Group Anagrams

Scenario: You need to group words that contain the same characters.

Strong answer: Sort each word's characters and use the sorted string as a map key, or use a character-count signature.

Why it works: Anagrams share the same normalized representation.

Common mistake: Comparing every pair of words, which is much slower.

Scenario 2: String Equality Bug

Scenario: new String("A") == "A" returns false.

Strong answer: Use .equals() for String content comparison. == compares object references.

Why it works: Two different String objects can contain the same characters.

Common mistake: Relying on string pool behavior instead of using equals().

Scenario 3: Validate Character Input

Scenario: A program asks for one character and needs to check whether it is a digit.

Strong answer: Read the string, take charAt(0) carefully, and use Character.isDigit(c).

Why it works: The Character utility class handles character classification clearly.

Common mistake: Comparing character ranges manually without considering readability or Unicode behavior.

Discussion

0 comments

Sign in to share a question or add to the discussion.
Start the discussion

Ask a question or share what stood out to you.