Second Largest Number

Problem Description
In high-speed data streaming and real-time analytics, we often need to track statistics on the fly without the luxury of storing every data point in a large memory buffer. Your task is to design a hardware module that monitors a continuous stream of unsigned integers and identifies the second-largest value encountered since the last system reset.
The Specification
Build a sequential Verilog module that processes an 8-bit input (DIN) on every clock cycle. The design must adhere to the following rules
Key Rules:
Maintain the two highest values seen so far. If a new input beats the current maximum, the old maximum is demoted to second place.
Duplicate Handling: Repeated values are treated as separate entries (e.g., in the sequence
7, 7, the second-largest is7).Startup Behavior: Output must remain
0until at least two numbers have been processed.Reset: An active-low
resetnsignal clears all history and restarts the output from0. (asynchronous active-low reset)
Input / Output Example
Watch how the "Silver Medalist" changes as the stream evolves:
Cycle | Input (DIN) | Output (DOUT) | Logic / Reason |
1 |
|
| Only one value seen; no second-largest exists. |
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
|
Final Sequence Trace:
Input: 2 5 1 8 6 5 0
Output: 0 2 2 5 6 6 6
Verilog
Code Editor Access
Log in to write, run and submit your solution to this interview question. It's free.
