Skip to main content

Practice ยท 1 of 2

The narrow seam

CHALLENGE
Difficulty: advanced+25 XP

Inside Solution, implement the engine contract: 1. public interface Engine { void put(String key, String value); java.util.Optional<String> get(String key); java.util.List<String> keys(); int count(); } 2. public static class InMemory implements Engine โ€” ConcurrentHashMap backed; keys() returns keys SORTED ascending (determinism is part of the contract). 3. public static class JsonlFile implements Engine โ€” constructor takes a java.nio.file.Path of an append-only file; put appends the line {"op":"put","k":"<key>","v":"<value>"}\n (UTF-8, CREATE+APPEND) then updates an internal ConcurrentHashMap index (write-through); get/keys/count read the index; keys() sorted ascending. 4. static Engine replay(Path file) โ€” a NEW JsonlFile whose state is rebuilt by reading every line of the existing file and applying each op in order (later puts overwrite earlier ones for the same key).

Back to lesson: Practice: Capstone build