Scrivio Interview library

Clean code and refactoring

19 questions in 6 subtopics.

Naming and readability

CodingFoundations7 min read
Rewrite this for readability and explain each change:
public List<int[]> getThem(List<int[]> l) {
List<int[]> r = new ArrayList<>();
for (int[] x : l) if (x[0] == 4) r.add(x);
return r;
}
LeadershipIntermediate8 min read
Your team's Spring services use inconsistent names: OrderManager, OrderHelper, OrderUtil, OrderServiceImpl and OrderProcessor all exist. What naming conventions would you agree as a team, and how do you apply them?

Code smells

DebuggingFoundations8 min read
Identify the code smells in this method signature and call, and suggest improvements:
void process(Order o, boolean sendEmail, boolean dryRun, String a, String b, int t);
process(order, true, false, "EU", "x1", 3);
ConceptIntermediate7 min read
Explain the code smells feature envy, shotgun surgery and divergent change, with examples from a Spring codebase and the refactorings that address each.

Refactoring techniques

ScenarioFoundations6 min read
You must change behaviour in a 400-line legacy method that has no tests. Describe how you refactor it safely before making the change.
CodingIntermediate7 min read
Refactor this conditional chain with modern Java:
double area(Object s) {
if (s instanceof Circle) {
Circle c = (Circle) s;
return Math.PI * c.r() * c.r();
} else if (s instanceof Rect) {
Rect r = (Rect) s;
return r.w() * r.h();
}
throw new IllegalArgumentException();
}
LeadershipAdvanced7 min read
Several teams say they never have time for refactoring, and technical debt slows every feature. How do you manage technical debt across teams?

Effective Java items

ConceptFoundations6 min read
Why should you prefer try-with-resources over try-finally for closing resources in Java?
DebuggingFoundations7 min read
A class overrides equals but not hashCode. Explain what goes wrong when instances are used as HashMap keys, and how to implement both correctly.
ConceptIntermediate7 min read
Effective Java advises favouring composition over inheritance and uses an example where a subclass of HashSet counts added elements by overriding add and addAll, and the count comes out double. Explain why and how composition fixes it.
ConceptIntermediate6 min read
What guidelines do you follow for Optional and for returning collections from methods? Explain why.
LeadershipPrincipal7 min read
Coding standards for a 500-engineer Java organisation, including Effective Java style rules: how detailed should they be, and how do you keep them useful for years?

API design for libraries

DesignIntermediate7 min read
Design the public API of an internal Java client library for the payments platform that 100 services will use. What principles guide it?
LeadershipAdvanced7 min read
You must make a breaking change to a shared library used by 120 services owned by many teams. Plan the migration.
LeadershipPrincipal7 min read
The JDK is known for long-term backward compatibility, using practices such as deprecation for removal, preview and incubator features, and strong encapsulation of internals. Which of these practices should guide your company's internal platform APIs over many years?

Code review

ConceptFoundations7 min read
What do you look for when reviewing a pull request in a Java service?
DebuggingIntermediate7 min read
Review this code and list the problems:
@Transactional
public void syncAll() {
for (Customer c : customerRepository.findAll()) {
try {
crmClient.push(c);
} catch (Exception e) { }
}
}
LeadershipAdvanced6 min read
Code review across your teams is slow and inconsistent: some pull requests wait days, and reviewers argue about formatting. How do you improve review culture across the organisation?
LeadershipPrincipal7 min read
AI coding assistants now produce a large share of code changes in many organisations. Over the next five years, how should your code review practices and clean code standards change?