Episode 7 — DSA with JavaScript / 7.2 — Loops Nested Loops Pattern Programming
7.2 — Interview Questions: Loops, Nested Loops & Patterns
Beginner
Q1. What is the difference between for, while, and do-while loops?
Answer:
| Feature | for | while | do-while |
|---|---|---|---|
| Structure | init; condition; update | condition only | condition at end |
| Min executions | 0 | 0 | 1 |
| Best for | Known count | Unknown count | Run-at-least-once |
// for: known count
for (let i = 0; i < 5; i++) console.log(i);
// while: unknown count
while (data.hasNext()) process(data.next());
// do-while: at least once
do { choice = getInput(); } while (choice !== "quit");
Q2. What is an infinite loop? How can it happen accidentally?
Answer: An infinite loop never terminates because its condition never becomes false.
Common causes:
- Forgetting to update the loop variable
- Wrong increment direction (++ instead of --)
- Floating-point comparison that never equals exactly
- Condition that can never be false
// BUG: i never changes
let i = 0;
while (i < 10) { console.log(i); } // infinite!
// FIX:
while (i < 10) { console.log(i); i++; }
Q3. How does break differ from continue?
Answer:
break: Exits the loop entirelycontinue: Skips the rest of the current iteration, goes to next
for (let i = 0; i < 5; i++) {
if (i === 2) continue; // skip 2
if (i === 4) break; // exit at 4
console.log(i);
}
// Output: 0, 1, 3
Q4. Write code to print a right-angled triangle of height n.
Answer:
function rightTriangle(n) {
for (let i = 1; i <= n; i++) {
console.log("*".repeat(i));
}
}
void rightTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) cout << "*";
cout << endl;
}
}
Q5. How do you iterate over an array in JavaScript vs C++?
Answer:
const arr = [10, 20, 30];
// for loop
for (let i = 0; i < arr.length; i++) { ... }
// for...of
for (const val of arr) { ... }
// forEach
arr.forEach((val, i) => { ... });
vector<int> arr = {10, 20, 30};
// Index-based
for (int i = 0; i < arr.size(); i++) { ... }
// Range-based
for (int val : arr) { ... }
// Iterator
for (auto it = arr.begin(); it != arr.end(); ++it) { ... }
Intermediate
Q6. How do you print a centered pyramid of stars?
Answer:
Row i (0-indexed) needs (n-1-i) spaces followed by (2i+1) stars.
function pyramid(n) {
for (let i = 0; i < n; i++) {
console.log(" ".repeat(n-1-i) + "*".repeat(2*i+1));
}
}
Dry-run for n=4:
Row 0: " *" (3 spaces, 1 star)
Row 1: " ***" (2 spaces, 3 stars)
Row 2: " *****" (1 space, 5 stars)
Row 3: "*******" (0 spaces, 7 stars)
Q7. What is the time complexity of printing an n×n pattern?
Answer: O(n²) — the outer loop runs n times, and for each iteration, the inner loop runs up to n times (or proportional to n). Total operations are proportional to n × n = n².
For a triangular pattern (1+2+3+...+n stars), the total is n(n+1)/2, which is still O(n²).
Q8. Print Pascal's triangle. Explain the algorithm.
Answer: Each element is C(i,j) = i! / (j! × (i-j)!). We can compute it incrementally:
function pascal(n) {
for (let i = 0; i < n; i++) {
let val = 1;
let row = " ".repeat(n - 1 - i);
for (let j = 0; j <= i; j++) {
row += val + " ";
val = val * (i - j) / (j + 1);
}
console.log(row);
}
}
Dry-run for n=5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Q9. How would you optimize a nested loop that searches for a pair with a given sum?
Answer:
// Brute force: O(n²)
function pairSum(arr, target) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] === target) return [i, j];
}
}
return null;
}
// Optimized with hash map: O(n)
function pairSum(arr, target) {
const seen = new Map();
for (let i = 0; i < arr.length; i++) {
const complement = target - arr[i];
if (seen.has(complement)) return [seen.get(complement), i];
seen.set(arr[i], i);
}
return null;
}
The hash map approach replaces the inner loop with an O(1) lookup.
Q10. Print a diamond pattern. Explain the symmetry.
Answer:
A diamond of height 2n-1 is a pyramid (n rows) followed by an inverted
pyramid (n-1 rows).
function diamond(n) {
for (let i = 0; i < n; i++) {
console.log(" ".repeat(n-1-i) + "*".repeat(2*i+1));
}
for (let i = n-2; i >= 0; i--) {
console.log(" ".repeat(n-1-i) + "*".repeat(2*i+1));
}
}
Advanced
Q11. Explain labeled loops. When are they useful?
Answer:
Labels allow break or continue to target an outer loop from within an
inner loop. They're useful for 2D searches where you need to exit both loops.
search:
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] === target) {
console.log(`Found at (${i}, ${j})`);
break search; // exits outer loop
}
}
}
C++ alternative: Use a boolean flag or return from a function.
Q12. How do you avoid O(n²) when working with nested data?
Answer: Common strategies:
- Hash maps — Replace inner loop with O(1) lookup
- Sorting + two pointers — O(n log n) instead of O(n²)
- Prefix sums — Pre-compute cumulative sums
- Early termination — Break as soon as answer is found
- Space-time tradeoff — Cache results to avoid recomputation
Q13. Implement Floyd's triangle. What is the pattern?
Answer: Floyd's triangle fills rows with consecutive natural numbers:
1
2 3
4 5 6
7 8 9 10
function floyds(n) {
let num = 1;
for (let i = 1; i <= n; i++) {
let row = "";
for (let j = 0; j < i; j++) {
row += (num++) + " ";
}
console.log(row.trim());
}
}
The total numbers printed is n(n+1)/2 (triangular number).
Q14. Write a spiral matrix printer.
Answer:
function spiralOrder(matrix) {
const result = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) result.push(matrix[top][i]);
top++;
for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) result.push(matrix[bottom][i]);
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
left++;
}
}
return result;
}
Time: O(m×n) | Space: O(1) excluding output
Quick-fire table
| # | Question | Answer |
|---|---|---|
| 1 | Loop that runs body first? | do-while |
| 2 | Exit inner loop only? | break (no label) |
| 3 | Skip current iteration? | continue |
| 4 | Time for nested n×n loop? | O(n²) |
| 5 | Stars in pyramid row i? | 2i+1 |
| 6 | Total stars in triangle? | n(n+1)/2 |
| 7 | How to exit outer loop from inner? | Labeled break (JS) or return |
| 8 | for...of vs for...in in JS? | of iterates values, in iterates keys |
| 9 | Range-based for in C++? | for (auto x : container) |
| 10 | Prevent infinite loop? | Ensure condition eventually becomes false |
Rapid self-check cards
RC-001
- Q: What are three types of loops?
- A: for, while, do-while
RC-002
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-003
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-004
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-005
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-006
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-007
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-008
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-009
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-010
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-011
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-012
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-013
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-014
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-015
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-016
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-017
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-018
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-019
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-020
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-021
- Q: What are three types of loops?
- A: for, while, do-while
RC-022
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-023
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-024
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-025
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-026
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-027
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-028
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-029
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-030
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-031
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-032
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-033
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-034
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-035
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-036
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-037
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-038
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-039
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-040
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-041
- Q: What are three types of loops?
- A: for, while, do-while
RC-042
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-043
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-044
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-045
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-046
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-047
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-048
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-049
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-050
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-051
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-052
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-053
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-054
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-055
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-056
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-057
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-058
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-059
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-060
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-061
- Q: What are three types of loops?
- A: for, while, do-while
RC-062
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-063
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-064
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-065
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-066
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-067
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-068
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-069
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-070
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-071
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-072
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-073
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-074
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-075
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-076
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-077
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-078
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-079
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-080
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-081
- Q: What are three types of loops?
- A: for, while, do-while
RC-082
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-083
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-084
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-085
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-086
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-087
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-088
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-089
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-090
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-091
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-092
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-093
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-094
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-095
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-096
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-097
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-098
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-099
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-100
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-101
- Q: What are three types of loops?
- A: for, while, do-while
RC-102
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-103
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-104
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-105
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-106
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-107
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-108
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-109
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-110
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-111
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-112
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-113
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-114
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-115
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-116
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-117
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-118
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-119
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-120
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-121
- Q: What are three types of loops?
- A: for, while, do-while
RC-122
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-123
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-124
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-125
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-126
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-127
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-128
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-129
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-130
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-131
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-132
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-133
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-134
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-135
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-136
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-137
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-138
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-139
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-140
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-141
- Q: What are three types of loops?
- A: for, while, do-while
RC-142
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-143
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-144
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-145
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-146
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-147
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-148
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-149
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-150
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-151
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-152
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-153
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-154
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-155
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-156
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-157
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-158
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-159
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-160
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-161
- Q: What are three types of loops?
- A: for, while, do-while
RC-162
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-163
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-164
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-165
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-166
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-167
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-168
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-169
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-170
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-171
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-172
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-173
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-174
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-175
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-176
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-177
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-178
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-179
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-180
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)
RC-181
- Q: What are three types of loops?
- A: for, while, do-while
RC-182
- Q: When is do-while better than while?
- A: When the body must execute at least once (menus, validation)
RC-183
- Q: How does break work in nested loops?
- A: Exits only the innermost loop
RC-184
- Q: What is the time complexity of printing a pyramid?
- A: O(n²) — two nested loops
RC-185
- Q: How many stars in row i of a pyramid (0-indexed)?
- A: 2*i + 1 stars
RC-186
- Q: What causes an infinite loop?
- A: Loop variable never reaches exit condition
RC-187
- Q: How do you skip an iteration?
- A: Use the continue statement
RC-188
- Q: What is a labeled break?
- A: A label lets break/continue target an outer loop
RC-189
- Q: How to iterate backwards?
- A: for (let i = n-1; i >= 0; i--)
RC-190
- Q: What is Floyd's triangle?
- A: Consecutive numbers filling rows: 1 / 2 3 / 4 5 6 / ...
RC-191
- Q: How to optimize nested loops?
- A: Use hash maps, sorting, prefix sums, or early termination
RC-192
- Q: What is the difference between for...of and for...in?
- A: for...of iterates values, for...in iterates enumerable property names
RC-193
- Q: How does continue work inside a for loop?
- A: Jumps to the update expression (i++), then checks the condition
RC-194
- Q: What is the safeguard against infinite loops?
- A: Maximum iteration counter with a throw/break if exceeded
RC-195
- Q: How to print a diamond pattern?
- A: Pyramid (n rows) + inverted pyramid (n-1 rows)
RC-196
- Q: What is the total iterations for a triangular pattern?
- A: n(n+1)/2 — triangular number
RC-197
- Q: How to exit two nested loops at once?
- A: Labeled break in JS, or use return from a function
RC-198
- Q: What is range-based for in C++?
- A: for (auto x : container) — iterates over elements directly
RC-199
- Q: How does forEach differ from for...of?
- A: forEach is a method with callback, for...of is a language statement that supports break
RC-200
- Q: When should you cache the loop bound?
- A: When the bound computation is expensive (arr.length is cheap, computed values are not)