Leaked Allocations: Filter Blocks Missing From Live Pointers
Arrays & HashingAuto-graded · 22 test casesLast updated
Filter a list of allocated block addresses to those absent from a list of live pointers, keeping the original order, using hash-set membership for linear-time performance.
Orphaned Memory Blocks
A tiny runtime tracks memory in two pieces:
allocated: a list of integers, each the starting address of a block currently allocated on the heap.live_pointers: a list of integers, each an address currently held by some variable in the call stack.
Blocks in this runtime never point at one another, so there is nothing to follow transitively. A block is in use exactly when its address appears in live_pointers. Every other block is orphaned.
Write a function that returns the addresses of all orphaned blocks.
Rules
- Output addresses in the same relative order in which they appear in
allocated. - Do not modify either input list.
live_pointersmay contain repeated values, and may contain addresses that are not inallocated. These have no effect on the result.- If
allocateditself contains the same address more than once, treat each occurrence independently: an orphaned address appears in the output once per occurrence, and an in-use address is removed in every occurrence. Do not deduplicate the output. - If nothing is in use, return a new list with the same contents as
allocated. If everything is in use, return an empty list.
Constraints
0 <= len(allocated) <= 10^50 <= len(live_pointers) <= 10^50 <= address <= 2^63 - 1- Aim for roughly linear time; comparing every block against every pointer is too slow at the upper limits.
Examples
Example 1
Input: find_orphaned_blocks([1000, 1008, 1016, 1024, 1032], [1008, 9999, 1008, 1032])
Output: [1000, 1016, 1024]
Example 2
Input: find_orphaned_blocks([16, 32], [])
Output: [16, 32]
Hints
Hint 1
Ask only one question about the pointers: is this address present? That suggests a structure with fast membership tests.
Hint 2
Build a set from live_pointers once. Repeated values collapse and unknown addresses are simply never looked up.
Hint 3
Walk through allocated from left to right and keep each address not in the set; this keeps the order and creates a new list rather than modifying the input.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The only thing we need to know about live_pointers is whether a given address is in it. So:
- Put all
live_pointersinto a hash set. This is done once, and repeated pointers collapse into one entry. - Scan
allocatedfrom left to right and collect every address that is not in the set.
Why it is correct
A block is in use exactly when its address occurs in live_pointers, and a set holds exactly the distinct values of that list, so addr in in_use is true precisely for in-use blocks. The scan therefore keeps exactly the orphaned ones.
- Order: iterating
allocatedin sequence and appending as we go preserves the original relative order. - Extra pointers: pointers to addresses outside
allocatedare in the set but never queried, so they have no effect. - Duplicates in
allocated: each occurrence is tested separately, so orphaned duplicates remain and in-use duplicates are all dropped. - No mutation: the set and the result list are new objects, so the inputs are untouched. An empty pointer list yields a fresh copy of
allocated. - Address 0 and large values: we test membership, not truthiness, so 0 is handled like any other integer.
Complexity
Let N = len(allocated) and M = len(live_pointers).
- Time: O(N + M) on average, since building the set costs O(M) and each of the N lookups costs O(1) on average.
- Space: O(M) for the set, plus the output list.
Checking membership directly against the list would cost O(N * M), up to about 10^10 operations, which is too slow.
Reference solution
from typing import List
def find_orphaned_blocks(allocated: List[int], live_pointers: List[int]) -> List[int]:
in_use = set(live_pointers)
return [addr for addr in allocated if addr not in in_use]