Deep Copy
Problem Description
You're given two classes — a small Header class nested inside a Packet class:
Header has:
- seq_id (int)
- protocol (string)
Packet has:
- id (int)
- hdr (a Header object)
Implement copy() inside Packet so it returns a brand new Packet
with the same values — including a brand new Header inside it.
Example:
Original p1:
id = 1
hdr → seq_id = 10, protocol = "AXI"
Call: p2 = p1.copy()
p2 now has:
id = 1
hdr → seq_id = 10, protocol = "AXI"
Now change p2.hdr.seq_id to 99.
p2.hdr.seq_id = 99 ✅ changed
p1.hdr.seq_id = 10 ✅ still the same
This only works if copy() creates a NEW Header for p2.
If you just do tmp.hdr = this.hdr both packets share
the same Header — changing p2 would also change p1.
SystemVerilog
Code Editor Access
Log in to write, run and submit your solution to this interview question. It's free.
