Lesson 1.1
Anatomy of a test problem
5 min · 7 steps
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.
Step 2 of 7 · Pick the approach
Problem: "Return how many numbers in
numsare greater thank."
Constraints:0 ≤ nums.length ≤ 100.Which part tells you that
numscan be empty?- The constraints
- The examples
- The function name
- The hidden tests
Step 3 of 7 · Predict the result
Problem: "Return how many numbers in
numsare greater thank." This code follows that statement. What does it return fornums = [4, 1, 7, 7]andk = 4?function solution(nums, k) { let count = 0 for (let i = 0; i < nums.length; i++) { if (nums[i] > k) count++ } return count}231
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.
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}0-2-8
Step 6 of 7 · Pick the approach
Problem: "Return how many words in
wordsare longer than 3 letters." Constraints:0 ≤ words.length ≤ 50.What should the function return for
words = []?0undefinednull[]
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 = 0often fail there. - The output type stays the same, even for empty input.