14. Sorting, Maps, Strings, and Character Checks | Java Backend Interview Prep - Study Chapter | QuizMaker

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

Read
13m
Type
Chapter
Access
Free

Course

Java Backend Interview Prep

Topic

3. OOP & DSA Revision

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.

Open on QuizMaker