Scrivio Interview library

Explicit locks

20 questions in 6 subtopics.

ReentrantLock

CodingFoundations6 min read
Show the correct usage pattern for ReentrantLock and list features it offers that synchronized does not.
DebuggingFoundations6 min read
What is wrong with this code?
try {
lock.lock();
if (!valid(order)) return;
process(order);
} finally {
lock.unlock();
}
Consider what happens if lock() throws or is interrupted, and compare with placing lock() before try.
ConceptIntermediate7 min read
On a Java 25 codebase, how would you choose between synchronized and ReentrantLock for new code, including when virtual threads are used?
ScenarioAdvanced8 min read
A new release of a shared client library introduced a global ReentrantLock around token refresh, and p99 latency rose tenfold across many services. You lead the incident. Describe the response and the follow-up changes.
LeadershipPrincipal8 min read
Over 15 years your codebase has accumulated synchronized blocks, ReentrantLocks, ReadWriteLocks, StampedLocks and custom lock wrappers. What would you standardise or forbid in locking, and how do you weigh consistency against letting experts optimise?

ReadWriteLock

ConceptFoundations6 min read
How does ReentrantReadWriteLock behave, and which of lock upgrade and lock downgrade does it support?
DebuggingFoundations6 min read
This cache method hangs the calling thread forever on a cache miss. Why?
rw.readLock().lock();
try {
V v = map.get(k);
if (v == null) {
rw.writeLock().lock();
try {
v = load(k);
map.put(k, v);
} finally {
rw.writeLock().unlock();
}
}
return v;
} finally {
rw.readLock().unlock();
}
ScenarioIntermediate7 min read
A team replaced synchronized with ReentrantReadWriteLock around a read-mostly map, expecting a speedup, but benchmarks show it got slower. Another service with the same lock sees writes delayed by seconds. Explain both.
DesignAdvanced7 min read
A shared reference-data library used by 40 services guards a large map with a ReentrantReadWriteLock; data reloads every five minutes. Some services see request latency spikes during reloads. Redesign the library.

StampedLock

CodingIntermediate7 min read
Write an optimistic read using StampedLock for a Point with fields x and y, and explain when the optimistic path falls back.
DebuggingIntermediate7 min read
A service that switched from ReentrantReadWriteLock to StampedLock started hanging under certain call paths where a method holding the write lock calls a helper that also takes the write lock. Explain and list StampedLock's other limitations.
LeadershipPrincipal7 min read
StampedLock offers fast optimistic reads but is not reentrant and is easy to misuse. How do you decide whether performance-oriented APIs with sharp edges should be allowed in a codebase that will live for ten years or more?

Condition

CodingFoundations7 min read
Implement put and take for a bounded buffer using ReentrantLock with two Conditions, and explain why two conditions are better than one.
CodingIntermediate7 min read
You need to wait until a condition becomes true or 500 ms pass, handling spurious wakeups correctly. Write the loop using Condition.awaitNanos.

Fairness

ConceptFoundations6 min read
What does a fair ReentrantLock guarantee, what does it cost, and does tryLock() respect fairness?
ScenarioIntermediate8 min read
Users report that a few requests wait seconds for a shared lock while most are fast. A teammate switched the lock to fair mode and p99 improved but throughput fell by 40%. How would you reason about this and what else would you try?

TryLock and timeouts

CodingFoundations7 min read
Write a money transfer between two Account objects that cannot deadlock, using either lock ordering or tryLock with timeout, and explain the trade-off.
ScenarioIntermediate7 min read
A hot code path uses lock.tryLock(200, MILLISECONDS) and on failure throws an exception that becomes an HTTP 500. During traffic spikes many requests fail this way. How would you treat the timeout and the underlying problem?
DesignAdvanced7 min read
A service used a ReentrantLock to make sure only one reservation per seat is created. After scaling to four instances behind a load balancer, double bookings appear. Design a correct approach and discuss the distributed-lock options.
ConceptPrincipal7 min read
In distributed systems, locks become leases with timeouts and fencing tokens. Which principles from in-JVM locking carry over, which do not, and what long-term stance on distributed locking would you take for your organisation?