Fundamentals/Patterns/Linear Scan8 problems· Arrays

Single pass through input making a per-element decision; no auxiliary data structure

When to useFind max/min, count satisfying condition, in-place transform, simple aggregations that don't need a Map/Set/Stack

iterative
Simulate Print Queue
simulate_print_queue
Time Needed to Buy Tickets
time_needed_to_buy_tickets
Smallest Integer Satisfying the Rule
google_oa_smallest_integer
Break a Palindrome
break_a_palindrome
Particle Velocity
particle_velocity
Winning Sequence
amazon_oa_winning_sequence
Candyhard
candy · no slots
Car Poolingmedium
car_pooling · no slots
INITIALIZE
accumulator / running tracker / result variable
const result = []; let time = 0;
let time = 0;
const result = digits.split('');
const n = s.length; if (n < 2) return ''; const chars = s.split('');
if (n < 3) return 0; let total = 0; let run = 1;
if (m < 2) return [];
const result = [];
TRAVERSE
for each element (or index) in input
for (const pages of jobs) {
for (let i = 0; i < tickets.length; i++) {
for (let i = 0; i < result.length; i++) {
for (let i = 0; i < Math.floor(n / 2); i++) {
for (let i = 1; i < n - 1; i++) {
for (let i = 0; i < n; i++) {
[PROCESS]
per-element decision: update accumulator
time += pages;
result.push(time);
const cap = i <= k ? tickets[k] : tickets[k] - 1;
time += Math.min(tickets[i], cap);
if (result[i] !== '?') continue;
for (const d of '123456789') {
const left = i > 0 ? result[i - 1] : '';
const right = i < result.length - 1 ? result[i + 1] : '';
if (d !== left && d !== right) { result[i] = d; break; }
}
if (chars[i] !== 'a') { chars[i] = 'a'; return chars.join(''); }
if (same diff) { run++; total += run - 1; } else { run = 1; }
if (i === 0) result.push(1);
else if (i === 1) result.push(2);
else {
const candidate = result[i - 1] + 1;
if (candidate <= m && candidate !== result[i - 2]) result.push(candidate);
else result.push(result[i - 2]);
}
RETURN
return accumulator
return result;
return time;
return result.join('');
chars[n - 1] = 'b'; return chars.join('');
return total >= 1e9 ? -1 : total;
return result;