Quick Overview

Given a disk page table where each page stores an in-use flag and a pointer to the next page of its file, find every page that starts a file after the master index page is lost. It tests modeling linked chains as a directed graph, ignoring stale pointers in free pages, and mapping between 0-based and 1-based indices.

Recover file head pages from a page table after the master page is lost

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a file system where page P0 is a special master page. It acts as the directory or index for the entire file system. Instead of standard file data, P0 stores the starting offsets (the head pointers) for every file currently stored on the disk. To read any file, the system must look up its starting page in P0 and then traverse the chain of `next_page_offset` pointers. Unfortunately, P0 has been corrupted and all head pointers are lost. You are given the page table (excluding P0) as an array `pages` where `pages[i] = [in_use, next_page_offset]` represents page `P(i+1)`. `in_use` is `1` if the page is active or `0` if it is free. `next_page_offset` is the 1-indexed page number of the next page in the file chain, or `-1` if it is the end of the file (EOF). Return a sorted list of all starting page indices that need to be restored into P0. A starting page is any in-use page that is not pointed to by the `next_page_offset` of any other in-use page. ### Function Signature ```python def recover_master_page(pages: list[list[int]]) -> list[int]: ``` ### Rules - `pages[0]` describes page P1, `pages[1]` describes P2, and so on. P0 is not in the array. - Only pointers stored in in-use pages count. The `next_page_offset` of a free page is stale data and is ignored, even when it names an in-use page. - A free page is never a starting page. - The in-use pages form file chains: no in-use page points to itself, and following pointers from any in-use page never loops back to a page already visited. - Return the starting page numbers (1-indexed) in ascending order. ### Constraints - `1 <= pages.length <= 10^5` - `pages[i][0]` is `0` or `1` - `pages[i][1]` is `-1` (EOF) or a valid 1-indexed page number, from `1` to `pages.length` - All `next_page_offset` values for in-use pages point to valid in-use pages or are `-1` - There is at least one in-use page ### Examples ```text Input: pages = [[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]] Output: [1, 4] ``` The in-use pages hold two files, P1 -> P3 -> EOF and P4 -> P5 -> EOF. P6 is free, so its stale pointer to P4 is ignored and P4 is still a starting page. ```text Input: pages = [[1, 2], [1, 3], [1, -1]] Output: [1] ``` There is a single file chain, P1 -> P2 -> P3 -> EOF. Only P1 is not referenced by any other page. ```text Input: pages = [[1, -1], [1, 1], [0, -1], [1, -1]] Output: [2, 4] ``` P2 points to P1, so the file is P2 -> P1 -> EOF and its start is P2 even though P1 has the smaller number. P4 is a one-page file that nothing points to, and P3 is free.

Overview: Given a disk page table where each page stores an in-use flag and a pointer to the next page of its file, find every page that starts a file after the master index page is lost. It tests modeling linked chains as a directed graph, ignoring stale pointers in free pages, and mapping between 0-based and 1-based indices.

A file system stores each file as a chain of pages. Page P0 is a special master page that serves as the directory for the whole disk: it stores the starting page (the head pointer) of every file currently on the disk. To read a file, the system looks up its starting page in P0 and then follows the chain of `next_page_offset` pointers. P0 has been corrupted and all head pointers are lost. You are given the page table without P0 as an array `pages`, where `pages[i] = [in_use, next_page_offset]` describes page `P(i+1)`. `in_use` is `1` if the page is active and `0` if it is free. `next_page_offset` is the 1-indexed page number of the next page in the file chain, or `-1` if the page is the end of its file (EOF). Implement `recover_master_page(pages)` and return a sorted list of all starting page numbers that need to be restored into P0. A starting page is any in-use page that is not pointed to by the `next_page_offset` of any other in-use page. Rules: - `pages[0]` describes page P1, `pages[1]` describes P2, and so on. P0 is not in the array. - Only pointers stored in in-use pages count. The `next_page_offset` of a free page is stale data and is ignored, even when it names an in-use page. - A free page is never a starting page. - The in-use pages form file chains: no in-use page points to itself, and following pointers from any in-use page never loops back to a page already visited. - Return the starting page numbers (1-indexed) in ascending order. Every page number fits in a 32-bit signed integer. Example 1: Input: pages = [[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]] Output: [1, 4] The in-use pages hold two files, P1 -> P3 -> EOF and P4 -> P5 -> EOF. P2 and P6 are free, so P6's stale pointer to P4 is ignored and P4 is still a starting page. Example 2: Input: pages = [[1, -1], [1, 1], [0, -1], [1, -1]] Output: [2, 4] P2 points to P1, so the file is P2 -> P1 -> EOF and its start is P2 even though P1 has the smaller number. P4 is a one-page file that nothing points to, and P3 is free.

Constraints

  • 1 <= pages.length <= 10^5
  • pages[i] = [in_use, next_page_offset] describes page P(i+1)
  • pages[i][0] is 0 or 1
  • pages[i][1] is -1 (EOF) or a valid 1-indexed page number, from 1 to pages.length
  • All next_page_offset values for in-use pages point to valid in-use pages or are -1
  • No in-use page points to itself, and following pointers from any in-use page never revisits a page
  • There is at least one in-use page

Examples

Input: ([[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]],)

Expected Output: [1, 4]

Explanation: Source example: files P1 -> P3 and P4 -> P5; free P6's stale pointer to P4 is ignored.

Input: ([[1, -1], [1, 1], [0, -1], [1, -1]],)

Expected Output: [2, 4]

Explanation: Source example: P2 -> P1 makes P2 the start, P4 is a one-page file and P3 is free.

Hints

  1. Ask what disqualifies an in-use page from being a start: it is named by the next_page_offset of another in-use page.
  2. A free page's pointer says nothing about the current files, so decide which rows' pointers you will look at before you count anything.
  3. Page numbers are 1-indexed while the array is 0-indexed. Check how you translate between the two, and remember the answer must list pages in ascending order.

Recover File Start Pages From a Corrupted Master Page

easyMicrosoftSoftware Engineer

A file system stores each file as a chain of pages. Page P0 is a special master page that serves as the directory for the whole disk: it stores the starting page (the head pointer) of every file currently on the disk. To read a file, the system looks up its starting page in P0 and then follows the chain of next_page_offset pointers.

P0 has been corrupted and all head pointers are lost. You are given the page table without P0 as an array pages, where pages[i] = [in_use, next_page_offset] describes page P(i+1). in_use is 1 if the page is active and 0 if it is free. next_page_offset is the 1-indexed page number of the next page in the file chain, or -1 if the page is the end of its file (EOF).

Implement recover_master_page(pages) and return a sorted list of all starting page numbers that need to be restored into P0. A starting page is any in-use page that is not pointed to by the next_page_offset of any other in-use page.

Rules:

  • pages[0] describes page P1, pages[1] describes P2, and so on. P0 is not in the array.
  • Only pointers stored in in-use pages count. The next_page_offset of a free page is stale data and is ignored, even when it names an in-use page.
  • A free page is never a starting page.
  • The in-use pages form file chains: no in-use page points to itself, and following pointers from any in-use page never loops back to a page already visited.
  • Return the starting page numbers (1-indexed) in ascending order. Every page number fits in a 32-bit signed integer.

Example 1: Input: pages = [[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]] Output: [1, 4] The in-use pages hold two files, P1 -> P3 -> EOF and P4 -> P5 -> EOF. P2 and P6 are free, so P6's stale pointer to P4 is ignored and P4 is still a starting page.

Example 2: Input: pages = [[1, -1], [1, 1], [0, -1], [1, -1]] Output: [2, 4] P2 points to P1, so the file is P2 -> P1 -> EOF and its start is P2 even though P1 has the smaller number. P4 is a one-page file that nothing points to, and P3 is free.

Examples

Example 1
Input
([[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]],)
Output
[1, 4]
Notes
Source example: files P1 -> P3 and P4 -> P5; free P6's stale pointer to P4 is ignored.
Example 2
Input
([[1, -1], [1, 1], [0, -1], [1, -1]],)
Output
[2, 4]
Notes
Source example: P2 -> P1 makes P2 the start, P4 is a one-page file and P3 is free.

Constraints

  • •1 <= pages.length <= 10^5
  • •pages[i] = [in_use, next_page_offset] describes page P(i+1)
  • •pages[i][0] is 0 or 1
  • •pages[i][1] is -1 (EOF) or a valid 1-indexed page number, from 1 to pages.length
  • •All next_page_offset values for in-use pages point to valid in-use pages or are -1
  • •No in-use page points to itself, and following pointers from any in-use page never revisits a page
  • •There is at least one in-use page

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def recover_master_page(pages: list[list[int]]) -> list[int]:
Sample call
recover_master_page([[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]])
Language
Case 1
Input
([[1, 3], [0, -1], [1, -1], [1, 5], [1, -1], [0, 4]],)
Expected
[1, 4]
Case 2
Input
([[1, -1], [1, 1], [0, -1], [1, -1]],)
Expected
[2, 4]

Browse More Questions

Show the approach

Approach

Use two linear passes over the page table with a boolean array pointed indexed by page number from 1 to n.

Pass 1: for every row whose in_use is 1 and whose next_page_offset is not -1, set pointed[next_page_offset] to true. Rows of free pages are skipped entirely, so a stale pointer never marks anything.

Pass 2: scan i from 0 to n - 1 and append i + 1 when row i is in use and pointed[i + 1] is false.

Invariant: after pass 1, pointed[p] is true exactly when some in-use page names page p. A page is a start exactly when it is in use and no in-use page names it, and pass 2 tests both conditions for every page, so the output holds every start and nothing else. Pass 2 visits pages in increasing number, so the list comes out in ascending order with no sort.

Edge cases: a single in-use page with EOF gives [1]. Free pages are never returned, even when nothing names them. A free page whose stale pointer names an in-use page leaves that page a start. When a chain runs backward (P2 -> P1), the higher-numbered page is the start. The array index is page number minus 1, so the last page n is handled through pointed[n] without going out of range. All values fit in 32-bit integers.

Time complexity:
O(n)
Space complexity:
O(n)