Skip to content
Read the problem

Lesson 1.1

Anatomy of a test problem

5 min · 7 steps

  1. Step 1 of 7 · Explanation

    A logic-test problem almost always has five parts: the statement, the signature (function name and parameters), examples of input and output, constraints, and hidden tests.

    Most people read the examples and start coding. But the answer to "which strange cases must I handle?" lives in the constraints.

  2. Step 2 of 7 · Pick the approach

    Problem: "Return how many numbers in nums are greater than k."
    Constraints: 0 ≤ nums.length ≤ 100.

    Which part tells you that nums can be empty?

    1. The constraints
    2. The examples
    3. The function name
    4. The hidden tests
  3. Step 3 of 7 · Predict the result

    Problem: "Return how many numbers in nums are greater than k." This code follows that statement. What does it return for nums = [4, 1, 7, 7] and k = 4?

    function solution(nums, k) {  let count = 0  for (let i = 0; i < nums.length; i++) {    if (nums[i] > k) count++  }  return count}
    1. 2
    2. 3
    3. 1
  4. Step 4 of 7 · Explanation

    The visible examples are usually easy. Hidden tests pick the edges from the constraints on purpose: an empty array, one item, negative numbers, equal numbers, the maximum size.

    If the constraints say numbers can be negative, assume a hidden test uses negative numbers.

  5. Step 5 of 7 · Predict the result

    Problem: "Return the largest number in nums." Constraints: 1 ≤ nums.length, -100 ≤ nums[i] ≤ 100. The example [3, 9, 2] passes. What does this code return for the hidden test [-5, -2, -8]?

    function solution(nums) {  let best = 0  for (let i = 0; i < nums.length; i++) {    if (nums[i] > best) best = nums[i]  }  return best}
    1. 0
    2. -2
    3. -8
  6. Step 6 of 7 · Pick the approach

    Problem: "Return how many words in words are longer than 3 letters." Constraints: 0 ≤ words.length ≤ 50.

    What should the function return for words = []?

    1. 0
    2. undefined
    3. null
    4. []
  7. Step 7 of 7 · Recap

    • A problem has five parts. Read all of them before coding, especially the constraints.
    • Small words like "greater than" decide between > and >=.
    • Hidden tests take their edges from the constraints. Starting values like best = 0 often fail there.
    • The output type stays the same, even for empty input.

    Pattern card

    Read the problem in five parts
    When to use it: Before writing the first line of code. Ask "what strange inputs do these constraints allow?"