Learn JavaScript

Lesson 6 of 9 · if, switch and Loops

Module 4 · if, switch and Loops

CP and Interview Pack: Nested Loops, Early Exit and the Bug Gallery

ProReading

Alice is in a mock interview, and her FizzBuzz just ran clean. The interviewer nods and asks the second question. "Now, given a list of numbers and a target, find the first pair that sums to the target." The loop is easy to start. The hard parts are what this lesson trains: which pairs to visit, how many there are, and how to stop at the answer. Exercise 3 is that exact question, graded.

Checking what this lesson needs for you…

What is inside CP and Interview Pack: Nested Loops, Early Exit and the Bug Gallery

  1. Counting a nested loop's passes
  2. The contest angle: O(n2) against a time limit
  3. Leaving two loops at once
  4. The off-by-one gallery
  5. FizzBuzz variations
  6. Six interview questions, with model answers