bugger.
All challenges/JavaScripthardmeta-sharded-feed-merge-v1
THE INVESTIGATION

The feed merge that spends unread cursors

Fill a unique feed page while advancing each shard cursor only for rows actually consumed.

Meta-styleInterview practiceAdvanced interviewsSharded pagination

What’s going wrong

Original Meta-style interview practice. This is not an actual Meta interview question. Bugger does not provide an AI assistant.

A feed service merges sorted shard snapshots. Its page may be short despite unread unique posts, and the returned cursors can skip posts that were only inspected. Repair readPage so lookahead, consumed rows and emitted posts have separate meanings.

Read {"shards":[[{"id":"a","timestamp":10}],...],"cursor":[0,...],"seenIds":[],"limit":2}. Each shard is already sorted by descending timestamp, then ascending ID using ASCII order. An ID may occur repeatedly within or across shards, always with the same timestamp. cursor has one integer position per shard and names its next unread row. seenIds contains IDs emitted by earlier pages.

Repeatedly inspect every unread shard head, choose the first in the global timestamp/ID order, and consume only that row by advancing its shard position once. Identical heads break ties by lower shard index. An already seen ID is consumed but does not fill a page slot. Otherwise emit its ID and add it to the seen set. Stop immediately when limit unique IDs have been emitted or every shard is exhausted; do not consume lookahead after filling the page.

Print {"ids":[...],"cursor":[...],"seenIds":[...],"hasMore":true}. seenIds is the union of input seen IDs and new IDs, sorted in ASCII order. hasMore is true exactly when some unconsumed row has an ID outside that updated set; calculating it must not move cursors.

Valid input has at most 8 shards and 200 total rows. IDs and seenIds are strings of 1 to 64 ASCII letters, digits, underscores or hyphens; input seenIds is unique and has at most 200 entries. Timestamps are integers from 0 to 1000000000. Each cursor is between zero and its shard length inclusive, and limit is an integer from 1 to 20. Empty shards and an empty shard list are valid. This is one deterministic snapshot; no networking or ranking changes occur.

Expected behavior

Fill a unique feed page while advancing each shard cursor only for rows actually consumed.

Reproduction

01 A losing shard head is still unread
{"shards":[[{"id":"a","timestamp":10},{"id":"c","timestamp":8}],[{"id":"b","timestamp":9}]],"cursor":[0,0],"seenIds":[],"limit":1}
{"ids":["a"],"cursor":[1,0],"seenIds":["a"],"hasMore":true}
02 One complete distinct shard control
{"shards":[[{"id":"a","timestamp":3},{"id":"b","timestamp":2}]],"cursor":[0],"seenIds":[],"limit":2}
{"ids":["a","b"],"cursor":[2],"seenIds":["a","b"],"hasMore":false}
03 Duplicate rows do not spend page slots
{"shards":[[{"id":"a","timestamp":10},{"id":"a","timestamp":10},{"id":"b","timestamp":9}]],"cursor":[0],"seenIds":[],"limit":2}
{"ids":["a","b"],"cursor":[3],"seenIds":["a","b"],"hasMore":false}

Before you begin

  • Merging sorted sequences
  • Cursor ownership and lookahead
  • Sets and stable ordering

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.