InterviewLegend

Trees & Graphs

Alex Chen34 min readUpdated 276 real interview reports

Trees and graphs sit in the middle of MongoDB's coding loop rather than at the front, but they earn real prep time because every shape here has an obvious home in a document database. A BSON document is a tree of fields. An index is a search tree the query planner walks. References between documents form a graph, and MongoDB even ships an aggregation stage that traverses one. So when an interviewer reaches for a tree or graph problem, it usually carries a whiff of that world, and the framing below leans into it.

The good news is that almost everything in this family runs off one skeleton: a depth-first or breadth-first walk that remembers where it has already been. Trees let you drop the visited set, because there are no cycles and every node has one parent. Graphs put it back, because references can point in circles and two paths can reach the same node. Get that one distinction reflexive and most of the cluster collapses into the same loop with different bookkeeping.

The MongoDB angle
A BSON document is a tree, which makes tree traversal the most native skill on this page. Every document is a root object whose fields hold scalars, arrays, or nested sub-documents, and a dot-notation path like address.city is just a walk from the root down to a leaf. When the database transforms, validates, or projects a document, it is running a depth-first pass over that tree. Interviewers who build this for a living tend to reward a candidate who traverses cleanly, names the empty and single-node cases before writing the loop, and keeps recursion depth in view. A pathologically nested document is a real input their engine has to survive without blowing the stack.
What's inside
  • 6 problems, worked step by step
  • about 34 min

Premium lessonMongoDB interview guide

These took digging.

What candidates walked out with, from 20,000+ reports, forum threads behind logins and write-ups in Chinese. Then solved step by step.

100 companies. Checked every month.

Unlock all 100 companies

$49 a year · less than $1 a week · 30-day money-back guarantee

Had your MongoDB interview?Tell us what they asked. Reviewed before it goes in, never with your name.
MongoDB interview guide

Know what MongoDB asks before you walk in.

31 coding · 5 system design · 15 behavioral questions

Get Premium$49 a year · 30-day money-back guarantee
Or start freeNo card.

Get their questions first.

InterviewLegend is independent and not affiliated with or endorsed by any company it covers.