A storage system keeps files as linked lists of pages. Each page has:
- a page offset or id,
- an empty bit indicating whether the page is unused, and
- the offset of the next page in the same file, or an EOF sentinel.
Page 0 is the master page. It normally stores the starting offsets, or heads, of all file chains. Page 0 has been corrupted, and you need to reconstruct it.
You are given pages, where each row is [id, emptyBit, next]. emptyBit is 1 for an empty page and 0 for a used page. next is the next page offset, or -1 for EOF.
Return the sorted list of starting offsets for all file chains. A starting offset is a used page id that is not pointed to by the next field of any other used page.
pages = [[1,0,3],[2,0,5],[3,0,10],[4,1,-1],[5,0,-1],[10,0,-1]] return = [1,2]
The used chains are 1 -> 3 -> 10 and 2 -> 5. Page 4 is empty, so it is ignored. The recovered master page offsets are [1,2].
pages = [[7,0,-1],[3,0,4],[4,0,-1],[9,1,-1]] return = [3,7]
Page 4 is pointed to by page 3, so it is not a head. Pages 3 and 7 are used and not pointed to by any used page.
1 <= pages.lengthpages[i].length == 3pages[i][1]is either0or1.pages[i][2] == -1orpages[i][2]is a page offset.
- Rank Open BusinessesPHONE SCREEN · Seen May 2026
- Retain Top K ValuesPHONE SCREEN · Seen May 2026
- In-Memory SQL with CSV InitializationONSITE INTERVIEW · Seen May 2026
- Order Records by Matching Start and EndONSITE INTERVIEW · Seen May 2026
- Get Minimum TimeSeen Jun 2025
- Count Subarrays with Bitwise OR PresentSeen Jun 2025
- Get Max Or SumSeen Jun 2025
- Maximum Number PossibleSeen May 2025
public int[] recoverMasterPage(int[][] pages) {
// write your code here
}