VLSI DV Interview Puzzles · All levels

Global Static vs Per-Specialization Static

Predict all printed `gid/lid` pairs and explain which counters are shared across `T` specializations.

Puzzle

Difficulty: Easy · Puzzle 1 of 6 · Topic: Parameterized Class Puzzles

Predict all printed `gid/lid` pairs and explain which counters are shared across `T` specializations.

Code

systemverilog
class id_bank;
  static int global_id = 0;
endclass

class bucket #(type T = int) extends id_bank;
  static int local_id = 0;
  int gid;
  int lid;
  function new();
    gid = global_id++;
    lid = local_id++;
  endfunction
endclass

initial begin
  bucket#(int)  a = new();
  bucket#(int)  b = new();
  bucket#(byte) c = new();
  bucket#(byte) d = new();
  $display("a gid/lid=%0d/%0d", a.gid, a.lid);
  $display("b gid/lid=%0d/%0d", b.gid, b.lid);
  $display("c gid/lid=%0d/%0d", c.gid, c.lid);
  $display("d gid/lid=%0d/%0d", d.gid, d.lid);
end

Hint

Track where each static is declared, not where constructor executes.

Step-by-step solution

diagram
1) `global_id` lives in non-parameterized `id_bank`, so all specializations share it: gids become 0,1,2,3.
2) `local_id` lives in `bucket#(T)`, so each specialization has its own static instance.
3) `bucket#(int)` lids are 0,1 and `bucket#(byte)` lids restart at 0,1.

Answer

Answer: Printed gids are 0/1/2/3 globally; lids are 0,1 for `int` and 0,1 for `byte` separately.

Why candidates get it wrong

Candidates often memorize 'statics are global' without considering parameterized specialization boundaries.

Interviewer follow-up

How would behavior change if `global_id` moved inside `bucket#(T)`?

Related topics