Analyze an Unbounded Session Cache and Thread Stop Flag
Quick Overview
Analyze Java cache growth, missing key equality, int remainder after overflow, and stop-flag visibility, then repair bounded IDs and thread termination.
Analyze an Unbounded Session Cache and Thread Stop Flag
Company: Millennium
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Analyze this Java program's key ID range, cache entry count, and termination behavior:
```java
static boolean stopped;
static class Key {
Integer id;
Key(Integer id) { this.id = id; }
@Override public int hashCode() { return id.hashCode(); }
}
// Inside main:
Map<Key, String> cache = new HashMap<>();
new Thread(() -> {
try {
Thread.sleep(24 * 60 * 60 * 1000);
stopped = true;
} catch (Exception e) {
} finally {
stopped = true;
}
}).start();
int i = 1;
while (!stopped) {
cache.put(new Key(i % 5000), "DATA" + i);
i++;
}
```
Explain why neither a bounded cache size nor termination after exactly one day follows from the code. Propose repairs for the intended bounded cache and visible stop signal.
### Clarifying Questions to Ask
- Should keys with the same integer ID identify the same cache entry?
- Is stopping after a duration a strict deadline or a best-effort signal?
- What should happen on interruption or integer counter overflow?
```hint Two independent contracts
Hash collisions do not imply key equality, and an unsynchronized write does not guarantee visibility to another thread.
```
### What a Strong Answer Covers
- Identity equality inherited from Object despite the overridden hashCode.
- Positive and negative Java remainder values after int overflow.
- Memory growth, possible allocation failure, and stop-flag visibility.
- A correct key type and a volatile or otherwise synchronized stop mechanism.
### Follow-up Questions
- Does making the map concurrent fix either key equality or stop visibility?
- How would you keep IDs nonnegative even if the counter overflows?
Overview: Analyze Java cache growth, missing key equality, int remainder after overflow, and stop-flag visibility, then repair bounded IDs and thread termination.
Analyze this Java program's key ID range, cache entry count, and termination behavior:
static boolean stopped;
static class Key {
Integer id;
Key(Integer id) { this.id = id; }
@Override public int hashCode() { return id.hashCode(); }
}
// Inside main:
Map<Key, String> cache = new HashMap<>();
new Thread(() -> {
try {
Thread.sleep(24 * 60 * 60 * 1000);
stopped = true;
} catch (Exception e) {
} finally {
stopped = true;
}
}).start();
int i = 1;
while (!stopped) {
cache.put(new Key(i % 5000), "DATA" + i);
i++;
}
Explain why neither a bounded cache size nor termination after exactly one day follows from the code. Propose repairs for the intended bounded cache and visible stop signal.
Clarifying Questions to Ask Guidance
Should keys with the same integer ID identify the same cache entry?
Is stopping after a duration a strict deadline or a best-effort signal?
What should happen on interruption or integer counter overflow?
What a Strong Answer Covers Guidance
Identity equality inherited from Object despite the overridden hashCode.
Positive and negative Java remainder values after int overflow.
Memory growth, possible allocation failure, and stop-flag visibility.
A correct key type and a volatile or otherwise synchronized stop mechanism.
Follow-up Questions Guidance
Does making the map concurrent fix either key equality or stop visibility?
How would you keep IDs nonnegative even if the counter overflows?