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
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.
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
- Ask what disqualifies an in-use page from being a start: it is named by the next_page_offset of another in-use page.
- A free page's pointer says nothing about the current files, so decide which rows' pointers you will look at before you count anything.
- 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.