Hardware interview practice
Report the top-K opcode frequencies
Count the opcode IDs in a completed packet frame and emit up to K distinct opcodes in descending frequency order, breaking equal counts by lower opcode ID.
Reviewed example
Work through one case
Input
accepted opcodes = [3, 1, 3, 2, 1, 3]; K=2Expected output
ranked outputs = [(opcode=3,count=3), (opcode=1,count=2)]Opcode 3 has the highest frequency and opcode 1 is the next distinct frequency winner.
What to cover
Requirements
- Support opcode IDs 0 to 15, frame lengths from 1 to 64, and K from 1 to 4 captured with the first item.
- Clear or invalidate all 16 histogram bins between frames.
- Do not select an opcode twice and never emit an absent opcode.
- If fewer than K distinct opcodes appear, emit only those present.
- Begin ranking only after the frame is complete and hold each output token stable while stalled.
