Verilog Interview Questions With Answers and Tested Code

Verilog interviews for design and verification roles keep returning to the same ideas: how the simulator schedules assignments, what hardware a block infers, and whether you can write a small circuit correctly the first time. Below are fifteen questions with the answer an interviewer wants to hear. Every design here was simulated in Icarus Verilog against a self-checking testbench and linted with Verilator.

Language and simulation

  • Q1. What is the difference between blocking (=) and nonblocking (<=) assignments? Answer: a blocking assignment updates its target immediately, so the next statement sees the new value; a nonblocking one evaluates its right-hand side now and updates at the end of the time step, after every block triggered on that edge has read its inputs. Use <= for flip-flops and = for combinational logic.
  • Q2. What is the difference between wire and reg? Answer: a wire is driven continuously (by assign or a port); a reg holds the last value assigned in a procedural block. reg does not mean a register in hardware: a reg assigned in always @(*) is combinational. In SystemVerilog, logic replaces both for most uses.
  • Q3. Why does a combinational always block infer a latch? Answer: if some path through it leaves an output unassigned, the output must keep its old value, which needs storage. Assign every output on every path, often with defaults at the top.
  • Q4. What is the difference between $display, $strobe and $monitor? Answer: $display prints when executed; $strobe prints at the end of the time step, after nonblocking updates; $monitor prints whenever any of its arguments changes.
  • Q5. What do === and == differ on? Answer: == returns x if either operand has x or z bits; === compares x and z literally and always returns 0 or 1. Use === in testbenches when checking for x.
  • Q6. What are casez and casex? Answer: casez treats z (written ?) as a don't-care; casex also treats x as a don't-care, which can hide x bugs in simulation, so casez is preferred.
  • Q7. What does a generate block do? Answer: it creates hardware at elaboration time, such as a loop of instances or a conditional choice set by a parameter.

Write this circuit

These are the small designs most often asked for at a whiteboard.

Q8. Swap two registers without a temporary

With nonblocking assignments both right-hand sides are read before either register changes, so a <= b; b <= a; swaps them on each clock. With blocking assignments both would end up holding b.

swap
// Q: swap two registers on every clock without a temporary.
module swap (input wire clk, input wire load, input wire [7:0] a0, input wire [7:0] b0,
             output reg [7:0] a, output reg [7:0] b);
  always @(posedge clk)
    if (load) begin a <= a0; b <= b0; end
    else begin a <= b; b <= a; end       // both read the old values
endmodule

Q9. Detect a rising edge

Register the input and compare: a one-clock pulse when the input is 1 now and was 0 on the last clock. If the input comes from another clock domain, synchronize it through two flip-flops first.

Rising-edge detectord is registered into d_q; pulse is d and not d_q, so it is high for exactly one clock after each rising edge of d.clkdd_qpulse
pulse = d & ~d_q: high for one clock at each rising edge, however long d stays high.
rise_detect
// Q: a one-clock pulse on each rising edge of a synchronous input.
module rise_detect (input wire clk, input wire rst, input wire d, output wire pulse);
  reg d_q;
  always @(posedge clk) d_q <= rst ? 1'b0 : d;
  assign pulse = d & ~d_q;
endmodule

Q10. Divide a clock by 3 with a 50% duty cycle

A divide-by-3 counter alone gives a 33% duty cycle. Make a pulse one input clock wide on the rising edge, delay it by half a clock with a falling-edge flip-flop, and OR the two: the output is high for 1.5 of every 3 input clocks. In real designs, prefer a clock enable or a PLL over generating clocks in logic.

div3
// Q: divide a clock by 3 with a 50% duty cycle.
module div3 (input wire clk, input wire rst, output wire clk_out);
  reg [1:0] cnt;
  reg pos, neg;
  always @(posedge clk) begin
    if (rst) cnt <= 2'd0;
    else cnt <= (cnt == 2'd2) ? 2'd0 : cnt + 2'd1;
  end
  always @(posedge clk) pos <= rst ? 1'b0 : (cnt == 2'd0);
  always @(negedge clk) neg <= rst ? 1'b0 : pos;
  assign clk_out = pos | neg;             // high for 1.5 of every 3 clocks
endmodule

Q11. Binary to Gray code and back

Gray code changes one bit per step, which is why it carries counter values across clock domains in an asynchronous FIFO. Binary to Gray is bin ^ (bin >> 1). Back again, each binary bit is the XOR of all Gray bits at or above it.

gray
// Q: binary to Gray code and back.
module gray #(parameter N = 4) (input wire [N-1:0] bin, output wire [N-1:0] g, output reg [N-1:0] back);
  integer i;
  assign g = bin ^ (bin >> 1);
  always @(*) begin                       // Gray to binary: each bit XORs all Gray bits above it
    back[N-1] = g[N-1];
    for (i = N - 2; i >= 0; i = i - 1) back[i] = back[i + 1] ^ g[i];
  end
endmodule

Q12. Count the ones in a byte

A for loop in a combinational block unrolls into an adder tree; it is not a loop in time.

popcount
// Q: count the 1s in a word (population count).
module popcount (input wire [7:0] x, output reg [3:0] n);
  integer i;
  always @(*) begin
    n = 4'd0;
    for (i = 0; i < 8; i = i + 1) n = n + {3'd0, x[i]};
  end
endmodule

Q13. A priority encoder

casez with ? lets the highest set bit win; the default covers no request.

prio
// Q: a priority encoder with casez: the highest set bit wins.
module prio (input wire [3:0] req, output reg [1:0] idx, output reg valid);
  always @(*) begin
    valid = 1'b1;
    casez (req)
      4'b1???: idx = 2'd3;
      4'b01??: idx = 2'd2;
      4'b001?: idx = 2'd1;
      4'b0001: idx = 2'd0;
      default: begin idx = 2'd0; valid = 1'b0; end
    endcase
  end
endmodule

The testbench checks each design: the swap after one clock, three pulses for three rising edges, the divide-by-3 duty cycle, every 4-bit Gray code both ways, all 256 bytes for popcount and all 16 request patterns. Icarus Verilog 12 printed:

Simulation output
swap: a = 34, b = 12 after one clock
rise_detect: 3 pulses for 3 rising edges
div3: high for 150 of 300 ns, duty about 50%
gray: 11 = 1011 in binary, 1110 in Gray
popcount: 10110101 has 5 ones
prio: req 0110 -> idx 2
errors = 0

Design judgment

  • Q14. Synchronous or asynchronous reset? Answer: a synchronous reset is a normal input sampled on the clock, so it is timed like data; an asynchronous reset acts immediately but its release must be synchronized to the clock, or flip-flops can come out of reset on different cycles. Many designs use asynchronous assert and synchronous release.
  • Q15. How do you pass a single bit safely to another clock domain? Answer: through a two-flip-flop synchronizer in the receiving domain, which gives a metastable first stage a full clock to resolve. Multi-bit values need a handshake, Gray-coded counters or an asynchronous FIFO, never a synchronizer per bit.

How to answer well

Say what hardware the code becomes, not only what it does in simulation. Interviewers listen for flip-flops versus combinational logic, reset behavior and clock domains. If you're asked to write code, state your assumptions (reset type, widths) first, then check your design with one example out loud, as the testbench does above.

Related

Sources

Last verified

All six designs were simulated in Icarus Verilog 12 and linted with Verilator 5.036 (-Wall, no warnings apart from the file-name rule) on 16 November 2026; the output above is from that run. The files are in the site's repository under scripts/interview.