Skip to the code
+25 XP

#391 One Page of a Merged Feed

medium

A home screen shows posts from several sources in one feed, newest first, a page at a time. Each source sends feeds[f], a list of { "id": ..., "time": ... } items already sorted newest first (largest time first).

Write feedPage(feeds, offset, limit) that returns the ids of the merged feed's items at positions offset to offset + limit - 1 (positions start at 0). The merged feed is ordered by time from largest to smallest; for equal times, items from a smaller feed index come first, and within one feed the original order is kept. If the feed runs out, return the ids that exist (possibly none).

Example 1

Input: feeds = [[{"id":"a1","time":90},{"id":"a2","time":50}],[{"id":"b1","time":70},{"id":"b2","time":60}]], offset = 1, limit = 2
Output: ["b1","b2"]

The merged order is a1 (90), b1 (70), b2 (60), a2 (50). Positions 1 and 2 are b1 and b2.

Example 2

Input: feeds = [[{"id":"x","time":5}],[{"id":"y","time":5}]], offset = 0, limit = 5
Output: ["x","y"]

The times tie, so the item from feed 0 comes first. Only two items exist.

Constraints

  • 0 <= feeds.length <= 10^5
  • 0 <= feeds[f].length <= 1000
  • All feeds together hold at most 10^5 items.
  • 0 <= offset, 1 <= limit <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).