Q256FreeSystemVerilog
Return every page at the kth density rank
Interview prompt
Question
Rank distinct page-density values from highest to lowest and return every 4 KiB page whose density equals the kth distinct level.
Starting point
Question code
function automatic void kth_dense_pages_4kb(
input bit [31:0] mem[longint unsigned],
input int unsigned k,
output longint unsigned pages_at_rank[$],
output int unsigned density
);Reviewed example
Trace one case
Input
page densities={0x1000:3,0x2000:1,0x3000:3,0x4000:2}; k=2Expected output
[0x4000]Distinct density ranks are 3, 2, and 1, so the second rank contains only page 0x4000.
What to cover
Requirements
- Treat equal densities as one rank.
- Use one-based k and return empty for k equal to zero or out of range.
- Return all pages at the selected rank.
- Sort equal-density outputs by page base.

