Scrivio Interview library

Generics

26 questions in 9 subtopics.

Type parameters and generic methods

ConceptFoundations5 min read
Why does List<Object> objs = new ArrayList<String>(); not compile, while Object[] arr = new String[1]; does?
CodingFoundations6 min read
Write a generic method that returns the first element of any list or a default value, and a generic class Pair<A, B>. Explain where the type parameter is declared in each case.
DesignIntermediate6 min read
Your team's repository layer has methods like Object find(String type, Object id) with casts at every call site. Design a generic, type-safe alternative and explain the trade-offs.
LeadershipPrincipal6 min read
Over twenty years your organization's codebases have mixed raw types, unchecked casts and generic APIs. How would you raise type safety across hundreds of repositories without stopping delivery?

Bounded type parameters

CodingFoundations6 min read
Write a method that returns the maximum of a collection of comparable elements. Why does the JDK's Collections.max declare <T extends Object & Comparable<? super T>>?
ConceptIntermediate6 min read
Explain how bounds affect what you can do inside a generic method, using <T extends Number> double sum(List<T> xs) versus double sum(List<? extends Number> xs). When do you need the type parameter?

Wildcards and PECS

CodingFoundations6 min read
Explain PECS with the signature of Collections.copy, and say which of these compile:
List<? extends Number> a = new ArrayList<Integer>();
a.add(1);
List<? super Integer> b = new ArrayList<Number>();
b.add(1);
Integer x = b.get(0);
CodingIntermediate6 min read
This helper fails to compile: static void swapFirstTwo(List<?> list) { list.set(0, list.get(1)); }. Explain the error and fix it with wildcard capture.
DebuggingIntermediate6 min read
A method declared as void addAll(List<Number> target, List<Integer> source) cannot be called with a List<Object> target or a List<Integer> target. Rewrite the signature to be as flexible as possible.
DesignAdvanced6 min read
You are reviewing a shared library's public API with signatures such as void process(List<Event> events) and List<? extends Event> fetch(). What wildcard guidelines would you apply?
LeadershipPrincipal7 min read
Generic signatures in shared libraries are permanent once published, and badly chosen ones spread through every caller. What review process and rules would you set for generic APIs in your organization's libraries?

Type erasure

ConceptFoundations7 min read
What is type erasure? Give three things you cannot do because of it.
InternalsIntermediate7 min read
If generics are erased, how do frameworks like Jackson, Spring and Hibernate learn that a field is List<Order>? Explain the reflection APIs and their limits.
ConceptPrincipal7 min read
Java chose erasure for generics in Java 5 while C# chose reified generics. Explain why, what it has cost the platform over two decades, and how Project Valhalla is addressing parts of it.

Bridge methods

InternalsIntermediate6 min read
Given class Name implements Comparable<Name> { public int compareTo(Name o) {...} }, what bridge method does javac generate and why? How can bridge methods appear in stack traces or reflection?
ScenarioAdvanced6 min read
A framework that scans methods by reflection registers each handler twice, or applies annotations inconsistently, for classes implementing generic interfaces. What is the cause and how do you fix the framework?

Reifiable types and generic arrays

DebuggingFoundations6 min read
This code compiles with a warning and throws ClassCastException at the call site, not inside the method. Why?
static <T> T[] toArray(List<T> list) {
return (T[]) list.toArray();
}
String[] s = toArray(List.of("a"));
ConceptIntermediate6 min read
What is a reifiable type? Why is new List<String>[10] illegal and how do you work around the need for a generic array, as ArrayList does internally?
DesignPrincipal6 min read
If Project Valhalla delivers value classes and later generic specialisation, collections of values could avoid boxing. How would you prepare a large codebase's generic APIs and data types today without depending on unfinished features?

Heap pollution and @SafeVarargs

ConceptIntermediate6 min read
What is heap pollution, how do generic varargs cause it, and when is it safe to annotate a method with @SafeVarargs?
LeadershipAdvanced6 min read
Your codebase has thousands of @SuppressWarnings("unchecked") annotations and several heap pollution incidents. What policy and tooling do you introduce?

Recursive generics

ConceptIntermediate6 min read
Explain the declaration abstract class Enum<E extends Enum<E>> implements Comparable<E>. What does the recursive bound guarantee?
DesignAdvanced7 min read
Design a fluent builder hierarchy for a base Request and a subclass HttpRequest using recursive generics, and discuss whether this complexity is worth it for a shared library.

Type inference

CodingFoundations6 min read
Which lines compile and what types are inferred?
List<String> a = new ArrayList<>();
var b = new ArrayList<>();
List<String> c = Collections.emptyList();
var d = List.of(1, 2.0);
DebuggingIntermediate5 min read
This compiles in Java 8 but a teammate's refactoring to a chained call fails to compile:
Comparator<User> c = Comparator.comparing(u -> u.getName()); // ok
Comparator<User> d = Comparator.comparing(u -> u.getName()).reversed(); // error
Explain and fix.
ConceptAdvanced6 min read
Explain how Java's type inference evolved from Java 5 to Java 10 (generic methods, diamond, target typing, var). What design limits remain and why did the language stop short of full inference?