Q283FreeSystemVerilog
Find the densest 4 KiB page
Interview prompt
Question
Given an associative array keyed by written addresses, return the 4 KiB-aligned page base containing the most keys and its density.

Starting point
Question code
function automatic void densest_page_4kb(
input bit [31:0] mem[longint unsigned],
output longint unsigned densest_page,
output int unsigned density
);Reviewed example
Trace one case
Input
written addresses=[0x1004,0x1010,0x1FFC,0x2000]Expected output
page_base=0x1000; density=3The first three keys share page number 1 after shifting by 12; the fourth belongs to page 0x2000.
What to cover
Requirements
- Count keys by shifting each address right by 12.
- Return the page base, not the page number.
- Return zero outputs for an empty input.
- Break equal-density ties in favor of the lower page base.

