Scrivio Interview library

Enums

18 questions in 6 subtopics.

Enum basics and values()

ConceptFoundations6 min read
What does the compiler generate for enum Status { ACTIVE, SUSPENDED }? Explain values(), valueOf(), ordinal() and name().
DebuggingFoundations7 min read
A service stores enum ordinal() values in a database column. After someone adds a constant in the middle of the enum, existing data maps to wrong statuses. Explain and fix.
ScenarioIntermediate6 min read
A hot path calls Status.values() inside a loop and a profiler shows allocation from it. Why, and what do you do?
ConceptPrincipal6 min read
Java added enums in Java 5, replacing int constants. Explain the design decisions behind Java enums (classes, not integers) and how later features (switch expressions, sealed types, records) changed how you would model closed sets today.

Enums with fields and behavior

CodingFoundations6 min read
Write an enum Operation with PLUS and TIMES, each holding a symbol and implementing apply(int a, int b), in two ways: constant-specific bodies and a lambda field.
DesignIntermediate6 min read
An enum maps external codes to constants with a static lookup map. Show the pattern and explain why a constructor cannot put itself into a static map of the same enum.
DesignAdvanced6 min read
A shared library exposes a public enum Country used by thirty services. A new value must be added every few months, and some services break each time. How do you redesign this?
LeadershipAdvanced7 min read
Teams put business rules in enums (pricing tiers with rates, state machines with transitions). When is that good design and when does it become a problem across many services?

EnumSet and EnumMap

ConceptFoundations6 min read
How are EnumSet and EnumMap implemented, and why are they faster than HashSet and HashMap with enum keys?
CodingIntermediate6 min read
Replace this flag representation with EnumSet and show how to persist it compactly: int permissions = READ | WRITE; // constants 1, 2, 4, 8

Enum singleton

CodingFoundations6 min read
Implement a singleton using an enum and explain why Effective Java calls it the best approach for many cases.
ScenarioIntermediate7 min read
A codebase uses enum singletons and static singletons for services such as caches and clients. Testing and configuration are painful. What would you propose?
ConceptPrincipal6 min read
Effective Java recommended the enum singleton, while modern Spring applications manage singletons through the container. How has the role of singletons changed in Java over twenty years, and what would you allow in new code?

Enum in switch

CodingFoundations5 min read
Write a switch expression over an enum Size { S, M, L } returning a price, without a default branch. What happens when a constant XL is added later?
DesignIntermediate6 min read
Should switches over enums include a default branch? Discuss the trade-off between compile-time exhaustiveness and run-time safety for enums that come from other services.

Enum serialization

InternalsIntermediate6 min read
How does Java serialization handle enums, and how do JSON libraries such as Jackson serialize them by default? What breaks when a constant is renamed?
ScenarioAdvanced7 min read
A service fails to deserialize events after the producer team added a new enum constant to a shared event schema. Several consumers crashed. How do you fix the incident and prevent recurrence?
LeadershipPrincipal6 min read
Across years of operation, enum values have been persisted as ordinals, names and custom codes in databases, caches and messages, causing several data incidents. What long-term rules would you set?