Second Largest Number

Intel

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 is 7).

  • Startup Behavior: Output must remain 0 until at least two numbers have been processed.

  • Reset: An active-low resetn signal clears all history and restarts the output from 0. (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

2

0

Only one value seen; no second-largest exists.

2

5

2

5 is now the max; 2 is demoted to second-largest.

3

1

2

1 is smaller than the current runner-up.

4

8

5

8 is the new max; 5 is demoted to second-largest.

5

6

6

6 is smaller than 8 but larger than 5.

6

5

6

5 is smaller than the runner-up.

7

0

6

0 is smaller than the runner-up.

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.

Output

Output goes here...