Stop 15 · Portfolio & Interview PrepTopics · 4/10

OOP & Collections Interview Questions

Part of the Java Roadmap

In short

"How does HashMap work internally?" might be the most asked Java interview question in India. Pair it with the four OOP pillars and interface vs abstract class, and you've covered a big chunk of a typical first round.

Interviewers often ask about the four OOP pillars, abstract class vs interface, overloading vs overriding, how HashMap works internally, and ArrayList vs LinkedList.

The jaadu as a postman slips letters into a wall of small letterboxes in an apartment lobby, peering at two letters that share one box.

Think of it like this

The apartment letterboxes

A housing society in Thane has a wall of 16 letterboxes. The postman uses the flat number to pick a box (the hash picks a bucket). Sometimes two flats share a box, so letters pile up inside and he checks names on each (a collision, resolved with equals). When boxes overflow, the society builds a bigger wall and re-sorts every letter (resizing).

Watch it happen

Putting keys into a HashMap with 8 buckets

0
1
2
3
4
5
6
7

1/5An empty table of 8 buckets. The bucket is chosen from the key's hashCode.

The big ideas

What you need to know

Four OOP pillars

Encapsulation, inheritance, polymorphism and abstraction. Give a short Java example for each, not just definitions.

Abstract class vs interface

An abstract class can hold state and constructors; a class extends only one. Interfaces define capabilities; a class can implement many.

equals and hashCode

Equal objects must have equal hash codes. Override both together, or HashMap and HashSet break.

ArrayList vs LinkedList

ArrayList has fast random access and is usually faster overall. LinkedList only wins for frequent inserts at the ends via an iterator or as a Deque.

See it in code

Walk through the code

java
1import java.util.*;2 3public class Interview {4    record Student(String rollNo, String name) {}   // records generate equals and hashCode5 6    static class Employee {7        final String id;8        Employee(String id) { this.id = id; }9 10        @Override public boolean equals(Object o) {11            return o instanceof Employee e && id.equals(e.id);12        }13        @Override public int hashCode() {14            return Objects.hash(id);15        }16    }17 18    public static void main(String[] args) {19        Map<Student, Integer> marks = new HashMap<>();20        marks.put(new Student("21CS07", "Meera"), 85);21        System.out.println(marks.get(new Student("21CS07", "Meera")));   // 8522 23        Set<Employee> team = new HashSet<>();24        team.add(new Employee("E1"));25        team.add(new Employee("E1"));26        System.out.println(team.size());   // 1, because equals and hashCode agree27    }28}

1/5A record gets correct equals and hashCode based on its components for free.

Remove hashCode from Employee and the set size becomes 2: two equal objects landing in different buckets.

Save yourself the pain

Common mistakes

Oops

Overriding equals but not hashCode.

Do this instead

Always override both. Equal objects must return the same hashCode, or hash-based collections misbehave.

Oops

Saying HashMap is thread-safe or ordered.

Do this instead

It's neither. Use ConcurrentHashMap for threads, LinkedHashMap for insertion order and TreeMap for sorted keys.

Oops

Using a mutable object as a key and changing it later.

Do this instead

If the fields used in hashCode change, the entry is lost in the wrong bucket. Use immutable keys.

Your mission

Explain HashMap on a whiteboard

~40 min

Quick check

Two objects are equal by equals() but return different hashCode values. What happens in a HashSet?

Go deeper

  1. 1Explain HashMap internals: hashing, buckets, collisions and resizing.
  2. 2Prepare real examples of each OOP pillar from your own projects.
  3. 3Review the equals and hashCode contract.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge