Scrivio Interview library

Sets

18 questions in 5 subtopics.

HashSet

InternalsFoundations6 min read
How is java.util.HashSet implemented, and how does add() report whether the element was new?
DebuggingFoundations6 min read
A developer writes
class Person {
String id;
public boolean equals(Person other){
return id.equals(other.id);
}
public int hashCode(){
return id.hashCode();
}
}
and finds that a HashSet<Person> holds duplicates with the same id. Why?
ScenarioIntermediate6 min read
A service keeps a HashSet<Long> of about 10 million user ids in memory and the heap is under pressure. Estimate the cost and propose alternatives.
LeadershipPrincipal6 min read
Your organisation has many memory-heavy Java services full of boxed collections such as Set<Long> and Map<Integer, Double>. Some teams want to adopt primitive collection libraries, others want to wait for Project Valhalla. How would you decide and what would you standardise?

LinkedHashSet

ConceptFoundations5 min read
When would you use LinkedHashSet, what is its cost, and what did Java 21's SequencedSet add?
CodingIntermediate6 min read
You must deduplicate a list of 5 million customer records by email while keeping the first occurrence and original order. Compare stream().distinct(), collecting into a LinkedHashSet, and a manual loop, including the parallel case.
ConceptPrincipal6 min read
Java 21 (JEP 431) retrofitted SequencedCollection, SequencedSet and SequencedMap onto existing types such as LinkedHashSet, List and Deque. Why did such a basic abstraction take until Java 21, and what does retrofitting interfaces onto widely implemented types teach about evolving your own long-lived APIs?

TreeSet and NavigableSet

ConceptFoundations6 min read
Name the NavigableSet operations of TreeSet that you cannot get from a HashSet, and give a practical use of one.
CodingFoundations5 min read
What does this print, and why?
Set<String> s = new TreeSet<>(Comparator.comparing(String::length));
s.add("abc");
s.add("xyz");
s.add("hello");
System.out.println(s);
ScenarioIntermediate7 min read
You maintain a live leaderboard of players in a TreeSet<Player> ordered by score descending, then id. Scores update often. Users report players appearing twice or not being found. What is wrong and how would you design it correctly?

EnumSet

InternalsFoundations8 min read
How is EnumSet implemented and why is it faster than a HashSet of enum constants?
DesignIntermediate8 min read
A legacy service stores user permissions as an int bit mask in the database and as Set<String> in code. You propose an enum Permission with EnumSet. How would you design storage and evolution so that adding or reordering enum constants is safe?
DesignAdvanced7 min read
A shared library defines an enum Feature and services exchange EnumSet<Feature> values over JSON and Kafka. Services deploy at different times, so a consumer may receive a feature name it does not know. Design the representation and evolution rules.

Set operations

CodingFoundations7 min read
Given two Set<String> a and b, write code to compute their union, intersection and difference without modifying a or b.
DebuggingIntermediate7 min read
A job calls hugeHashSet.removeAll(idList), where hugeHashSet has 200,000 elements and idList is an ArrayList of 1 million ids. It takes minutes. Explain why and fix it.
ScenarioAdvanced8 min read
After upgrading code to use Set.of and Map.of for constants, some services produce JSON whose array order changes between restarts, breaking response caching and a few consumer tests. How would you handle this across teams?
ScenarioAdvanced7 min read
An audience-targeting service intersects sets of millions of user ids per request (for example users in region X who also bought Y), using HashSet<Long> and retainAll. Latency and memory are growing as data grows. What would you change architecturally?
LeadershipPrincipal6 min read
Your codebase uses JDK collections, Guava immutable collections and Eclipse Collections in different places. The JDK has since added List.of and Set.of (9), copyOf (10) and sequenced collections (21). What long-term policy on collection libraries would you set?