0 day streak
Anonymous · shared experience20 Mar 2026 · 4 views

MongoDB: Versioned document store with out-of-order writes

Interview question #MongoDB SDE 3 / Senior DSA / coding

Problem. Build a small datastore with two operations. put(docId, contents, timestamp) saves a version of a document at a timestamp. One document can have many versions, and puts may arrive with timestamps in any order. get(docId, timestamp) returns the contents of the latest version strictly before the given timestamp, or an empty string if there is none.

Example. After put(1, "abc", 10), put(2, "bcd", 11), put(1, "cde", 8) and put(2, "def", 9): get(1, 5) returns "", get(1, 9) returns "cde", and get(2, 15) returns "bcd".

What mattered. Because writes are not ordered, a plain appended list with binary search is not enough unless you keep it sorted. The candidate kept an ordered tree per document so both operations stay logarithmic, and wrote a simple unbalanced tree since their language had no built-in balanced one. A sorted map with a floor lookup is the natural fit in languages that have one.

0 comments

Comments 0

No comments yet. Ask the author something, or add what happened in your own round.