A dry run means executing your code by hand, on paper, one line at a time, writing down what every variable holds. It's the single best habit for understanding algorithms and for catching bugs before the computer does — and in interviews you'll be asked to "walk me through an example", which is exactly this.
second receive the old largest teaches you why the algorithm works.Here is a classic warm-up — finding the second largest element — dry-run on [12, 35, 1, 10, 34]:
let largest = -Infinity, second = -Infinity;for (let i = 0; i < nums.length; i++) { if (nums[i] > largest) { second = largest; largest = nums[i]; } else if (nums[i] < largest && nums[i] > second) { second = nums[i]; }}Look at step 2. If the code had forgotten second = largest, the table would show second still at -∞ there — and you'd spot the bug immediately, long before any test failed.
Every problem page has a dry-run simulator that does this table for you, so you can check your own paper dry run. Here's how to use one:
3, 1, 4), or pick one of the Try examples. Press Run.i, left and write shown above the cells. Colours mark the current element, finished parts, and the answer.Try it now. Before pressing Next, predict how largest and second will change, then check yourself. Then change the input to [10, 5, 10] and see how the duplicate is handled.
Predict each step first, then press Next to check — exactly like a paper dry run.