← Design Verification Interviews
Interview Question · Code Writing
Removing Every Match from a Queue
The Question
The interviewer asks
Write code that removes every 5 from an int queue. A candidate wrote the loop below; what is left in q afterwards?
int q[$] = '{5, 5, 3, 5, 7};
// remove every 5
for (int i = 0; i < q.size(); i++)
if (q[i] == 5) q.delete(i);You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
It leaves 5 3 7. Each delete(i) shifts the later elements down by one. So the second of two neighbouring 5s lands at index i, and the loop moves past it. Write the result instead of editing in place: q = q.find(x) with (x != 5);. If you must delete in a loop, walk the indexes from the end down to 0.
Each delete shifts the later elements down, so the element after a deleted 5 is never checked.
Self-Check
Tick each key point you covered in your answer.
What Interviewers Listen For (Fresher)
A good fresher answer traces the indexes by hand and finds the element that the loop skips.
What Interviewers Listen For (Junior)
A good junior answer uses a locator method with a with clause. It knows that the result is a new queue.
What Interviewers Listen For (Mid-Level)
A good mid-level answer notes that many deletes in the middle of a large queue are slow. It keeps scoreboards to pops at the front.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
What IfProve ItBreak ItCompare
- Removing Every Match from a Queue
-
CompareLocator Methods vs a Hand-Written LoopL1 Junior
Compare
q.find(x) with (x != 5)with a loop that pushes the survivors into a new queue.2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Both build a new queue and both are linear. The locator method states the intent in one line and cannot get an index wrong. The loop can do more in one pass, such as count and log the removed items. In reviews, prefer the method unless the loop does more than filter.
Self-Check
Tick each key point you covered in your answer.
Half-Truth
"Locator methods are faster because they are built in."
The standard sets no complexity for them. Choose them for clarity, and measure if speed matters.
Source IEEE 1800-2017 §7.12
-
Break ItDeleting from a Queue Inside a LoopL1 Junior
Break it: a loop deletes matches in place with
q.delete(i)whileicounts up. What goes wrong?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
After
q.delete(i), every later element moves down one index. The loop then adds 1 toiand skips the element that moved into sloti. So two matches in a row leave the second one behind. Loop down from the end, or build a new queue.Self-Check
Tick each key point you covered in your answer.
Misconception
"Use
foreachand delete inside it; the loop follows the size."The standard leaves the result undefined if the array size changes during a
foreach. Tools may skip or repeat elements.Source IEEE 1800-2017 §12.7.3
-
What IfCounting with Sum on a Bit ArrayL1 JuniorL2 Mid-Level
Asked if they count matches with
sum()What if they count set flags with
flags.sum()on abit flags[8], or total byte lengths withlens.sum()? Predict the output.systemverilog module tb; bit flags [8] = '{default: 1}; // eight set flags byte lens [] = '{100, 100}; // two lengths of 100 initial begin $display("flags.sum()=%0d with int=%0d", flags.sum(), flags.sum() with (int'(item))); $display("lens.sum()=%0d with int=%0d", lens.sum(), lens.sum() with (int'(item))); end endmodule2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
It prints
flags.sum()=0 with int=8andlens.sum()=-56 with int=200. A reduction method returns the element type, so eight 1-bit flags add up modulo 2 and two signed bytes overflow. Cast each item in awithclause, such asflags.sum() with (int'(item)), to make the sum wide enough.Three Questions Asked of One ArrayThree method calls on an associative array age, keyed by read ID. Arrows show data: the values each method reads and the result it returns. Step 2 shows the cast that makes a sum wide enough.Illustrative sequenceCallValueReturnWaiting- Search for stuck reads
itemstands for each value in turn, and two values exceed 1,000. On an associative arrayfind_indexreturns their keys, IDs 7 and 12, as a queue.Follow the calls, values and waits
- data 1500 and 2048 match: age [int] → find_index
- data {7, 12}: find_index → ids
- Count them
The cast inside the
withclause turns each comparison into a 32-bit 0 or 1, so the sum is 2. Without the cast, the sum is 1 bit wide and 1 + 1 wraps to 0.Follow the calls, values and waits
- data 0, 1, 0, 1: age [int] → sum
- data 2: sum → n
- An empty answer
No value exceeds 5,000, so
findreturns an empty queue. Checksize()before reading element 0 of any result.Follow the calls, values and waits
- data no match: age [int] → find
- data {}: find → ages
- The array is unchanged
None of these calls changed
age. Locator and reduction methods only read the collection;sortand the other reordering methods change it.
Read the diagram as text
- age [int]: ID 3: 12, ID 7: 1500, ID 9: 40, ID 12: 2048
- find_index: with (item > 1000)
- sum: with (int'(item > 1000))
- find: with (item > 5000)
- ids: queue {7, 12}
- n: int: 2
- ages: queue {}, size() is 0
- age [int] → find_index
- age [int] → sum
- age [int] → find
- find_index → ids: keys
- sum → n: one value
- find → ages: values
Every locator method returns a queue, even when the answer has one element or none.
RUNVerilator · simulator serviceExpected:flags.sum()=0 with int=8lens.sum()=-56 with int=200Self-Check
Tick each key point you covered in your answer.
Misconception
"
sum()returns anint, like most functions."Its result has the type of the array elements, unless a
withexpression gives another type. So abitarray sums in one bit.Source IEEE 1800-2017 §7.12.3
- Search for stuck reads
-
Prove ItSolve 2.4.2: Wide Population ReductionSimulator-graded
Show it, don't just say it: this Silicon Lab problem is graded by the simulator.
-
Prove ItSolve 2.2.2: Bounded Queue OperationsSimulator-graded
Show it, don't just say it: this Silicon Lab problem is graded by the simulator.
-
Break ItPopping an Empty QueueL1 Junior
A scoreboard calls
exp_q.pop_front()when a response arrives. How does this break when the DUT sends a response nobody asked for?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
Popping an empty queue returns the element type's default, perhaps with a warning, and the scoreboard compares the response against that default. A spurious response of value 0 then passes. Check
exp_q.size()first and report an unexpected response.Self-Check
Tick each key point you covered in your answer.
Misconception
"An empty pop stops the simulation with an error."
The standard returns the default value and at most warns, so the test keeps running with a made-up expectation.
Source IEEE 1800-2017 §7.10.2.4
What People Get Wrong
Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.
Misconception
"Deleting inside the index loop removes every match."
The indexes move under the loop. The twin starts with 5 5 3 5 7 and leaves a 5 behind.
Run It
The candidate's loop on 5 5 3 5 7.
left: 5 3 7 FAIL: each delete shifted the next 5 into the slot the loop had already passedSee the Fix
module tb;
int q[$] = '{5, 5, 3, 5, 7};
string s = "";
initial begin
q = q.find(x) with (x != 5); // the fix: build the survivors, no index juggling
foreach (q[i]) s = {s, $sformatf(" %0d", q[i])};
$display("left:%s", s);
if (5 inside {q}) $display("FAIL: each delete shifted the next 5 into the slot the loop had already passed");
else $display("PASS: no 5 is left");
end
endmodule
Prints: left: 3 7 PASS: no 5 is left
Tool as Truth
"Use foreach with delete; foreach handles the shifting."
Changing a collection's size inside a foreach over it is undefined. One simulator may happen to give the right answer; another need not.
Source IEEE 1800-2017 §12.7.3
Spec Misquote
"find returns the indexes of the matches."
find returns the matching elements; find_index returns their indexes. Both return a queue, empty when nothing matches.
Source IEEE 1800-2017 §7.12.1