# MongoDB interview prep

## Which index gets the newest 20 orders onto the screen?

Original Opend practice case · Updated 2026-11-30

[Cite this case](https://opend.in/prep/mongodb#compound-index)

A merchant dashboard reads one tenant’s open orders, sorted by creation time descending, with a page size of 20. The collection has millions of documents. A second dashboard adds a minimum order total. Do these screens need the same index?

**Short answer** — Use the exact query shape to propose a compound index. Compare keys and documents examined, sort work and write cost on production-shaped data before deciding.

### Read one tenant’s newest open orders

- **Seek the prefix** — tenantId = T9, status = open. These equality fields identify one contiguous index interval.

- **Read in order** — createdAt descending gives the requested order within that interval.

- **Stop after 20** — Fetch the matching documents for display. Add a stable tie-breaker if the result is paginated.

### Translate the query into an access path

For the first screen, try { tenantId: 1, status: 1, createdAt: -1 }. A single-field time index could also return recent orders in order, but it may walk past many other merchants’ records. With a tenant-and-status prefix, the scan starts inside the relevant interval. If two orders can share a timestamp, include a unique tie-breaker in both the sort and the index when implementing a cursor.

### Use a second workload to challenge the first design

Now require total above a high threshold. Putting the range field after the time field retains the requested order, but the engine might scan many entries to find 20 large orders. Putting a highly selective range before the sort can reduce the candidate set enough to make a small in-memory sort worthwhile. MongoDB’s equality, sort and range guidance describes both choices.

[MongoDB compound index ordering](https://www.mongodb.com/docs/manual/tutorial/equality-sort-range-guideline/)

### Explain what the measurements can prove

The sample runs below return the same 20 full documents. The compound index examines one key per result in this simplified case. That ratio says more about avoidable work than a single warm-cache timing. Inspect the winning plan for a blocking sort, then repeat with a large tenant and a sparse tenant. Finally measure representative writes and index size. Keeping a second index is a workload decision with an ongoing write cost.

[MongoDB execution statistics](https://www.mongodb.com/docs/manual/reference/explain-results/)

### Illustrative execution stats for the first dashboard

| Plan | Keys / docs examined | Returned |
| --- | --- | --- |
| Time index only | 820 / 820 | 20 |
| Tenant, status, time | 20 / 20 | 20 |
| Collection scan and sort | 0 / 40,000 | 20 |

### A colleague sees zero keys examined and declares the collection scan fastest. How do you respond?

Read totalDocsExamined and the plan stages with the key count. The collection scan visited 40,000 documents. Zero index keys simply identifies the access path. To choose a plan, compare result correctness, resource use and repeated timings under representative traffic.

MongoDB's engineering careers page describes an initial technical interview within its recruiting process. Confirm the rest of your loop with the recruiter; the topics below are Opend's practice recommendations.

## Coding & problem solving

Practice a small, testable solution. Explain data structures, complexity and edge cases aloud.

## LLD / HLD

Reason about index choices, read/write tradeoffs, consistency and failure recovery using explicit workloads.

## Practical systems & work discussion

Walk through one project you shipped: the constraint, your decision, a failure, and the result you can substantiate.

## Practice questions

These are Opend practice prompts, not verified interview questions.

### How would you choose an index for a slow query?

- Start with query predicates, sorting, selectivity and the execution plan.
- Compare examined documents to returned documents, then test a compound index against real workloads.
- Account for write amplification and storage cost before adding more indexes.

### What changes when a replicated database loses its primary?

- State the consistency and durability requirements before discussing elections.
- Distinguish acknowledged writes from replicated writes and explain possible retry ambiguity.
- Test partitions, delayed replicas and client retry behavior; avoid promising exactly-once delivery.

### When would you embed data instead of referencing it?

- Start from access patterns, update frequency and bounded document size.
- Compare atomic updates and read locality against duplication and unbounded growth.
- Show how your choice changes when the workload or cardinality changes.

[Live roles](https://opend.in/jobs?company=MongoDB)

## Sources

- [MongoDB: engineering recruitment](https://www.mongodb.com/company/careers/teams/engineering)
