Ctrl K
Java · Junior · Theory

What is the equals and hashCode contract, and what happens if you break it?

~4 min by voice

How to approach it

They're checking whether the candidate understands how equals and hashCode are linked and why hash collections (HashMap, HashSet) depend on that link. Not just "I generate them in the IDE", but what exactly breaks when the contract is violated, and why.

Have you seen this question in a real interview?
Answers0
Avg. score—
Strong—
Practiced0
Topics
Similar questionsWrite a class FlatIterator that takes a list of lists of integers and implements the Iterator interface: hasNext tells you whether there are elements left, and next returns the next number, going through all the nested lists in order. You're not allowed to copy all the elements into one combined list up front. Mid-levelImplement an LruCache class with a fixed capacity passed to the constructor. The get method returns the value for a key or null, the put method stores a key-value pair, and when the cache overflows, the element that hasn't been accessed for the longest time is evicted. Both operations must run in constant time. Mid-levelThere's a class Point with two integer fields, x and y. Override its equals and hashCode methods so that points with the same coordinates are considered equal and work correctly as keys in a HashMap. JuniorWhat's the difference between ArrayList and LinkedList, and when would you choose each? JuniorHow does a HashMap work internally? What happens when you call put? Junior

Answer by voice

The interviewer will ask this question and listen. Think out loud and ask for a hint if you need one. When you're done, pause or tap “That's my answer”. In about a minute, your score and the points you covered show up here.

Sign in to answer

Best with headphones · ~4 min