Skip to main content

Beginner JavaScript Problems 1–100

Build correct mental models before optimizing. Solve each problem first with explicit readable JavaScript, then compare your implementation with the expandable approach.

1. Add Two Numbers

Difficulty: Beginner · Category: Arithmetic

Problem statement: Return the sum of two numeric inputs.

Input: two finite numbers

Output: one number

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 2, 3 → 5

Hint: Identify the invariant or transformation first; add the operands directly.

Recommended approach: Add the operands directly.

Complexity: O(1) time and O(1) space.

Solution explanation

Add the operands directly. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

2. Safe Division

Difficulty: Beginner · Category: Arithmetic

Problem statement: Divide two numbers and reject a zero divisor.

Input: dividend and divisor

Output: quotient or error

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 10, 2 → 5

Hint: Identify the invariant or transformation first; guard the divisor before division.

Recommended approach: Guard the divisor before division.

Complexity: O(1) time and O(1) space.

Solution explanation

Guard the divisor before division. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

3. Square and Cube

Difficulty: Beginner · Category: Arithmetic

Problem statement: Return both the square and cube of a number.

Input: one finite number

Output: object with square and cube

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 3 → {square: 9, cube: 27}

Hint: Identify the invariant or transformation first; compute powers with multiplication.

Recommended approach: Compute powers with multiplication.

Complexity: O(1) time and O(1) space.

Solution explanation

Compute powers with multiplication. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

4. Swap Two Values

Difficulty: Beginner · Category: Arithmetic

Problem statement: Return two values in reversed positions without global state.

Input: two values

Output: reversed pair

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 3, 10 → 10, 3

Hint: Identify the invariant or transformation first; use destructuring or a temporary binding.

Recommended approach: Use destructuring or a temporary binding.

Complexity: O(1) time and O(1) space.

Solution explanation

Use destructuring or a temporary binding. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

5. Even or Odd

Difficulty: Beginner · Category: Arithmetic

Problem statement: Classify an integer as even or odd.

Input: one integer

Output: even or odd label

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 7 → odd

Hint: Identify the invariant or transformation first; use the remainder operator.

Recommended approach: Use the remainder operator.

Complexity: O(1) time and O(1) space.

Solution explanation

Use the remainder operator. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

6. Number Sign

Difficulty: Beginner · Category: Control Flow

Problem statement: Classify a number as positive, negative or zero.

Input: one number

Output: sign label

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: -4 → negative

Hint: Identify the invariant or transformation first; use ordered comparisons and an explicit zero branch.

Recommended approach: Use ordered comparisons and an explicit zero branch.

Complexity: O(1) time and O(1) space.

Solution explanation

Use ordered comparisons and an explicit zero branch. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

7. Largest of Two

Difficulty: Beginner · Category: Control Flow

Problem statement: Return the larger of two numbers and handle equality.

Input: two numbers

Output: largest value

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 8, 8 → 8

Hint: Identify the invariant or transformation first; compare once or use Math.max.

Recommended approach: Compare once or use Math.max.

Complexity: O(1) time and O(1) space.

Solution explanation

Compare once or use Math.max. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

8. Largest of Three

Difficulty: Beginner · Category: Control Flow

Problem statement: Return the largest of three finite numbers.

Input: three numbers

Output: largest value

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 4, 9, 2 → 9

Hint: Identify the invariant or transformation first; track a running maximum.

Recommended approach: Track a running maximum.

Complexity: O(1) time and O(1) space.

Solution explanation

Track a running maximum. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

9. Smallest of Three

Difficulty: Beginner · Category: Control Flow

Problem statement: Return the smallest of three finite numbers.

Input: three numbers

Output: smallest value

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 4, 9, 2 → 2

Hint: Identify the invariant or transformation first; track a running minimum.

Recommended approach: Track a running minimum.

Complexity: O(1) time and O(1) space.

Solution explanation

Track a running minimum. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

10. Leap Year

Difficulty: Beginner · Category: Dates

Problem statement: Determine whether a Gregorian calendar year is a leap year.

Input: integer year

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 2024 → true

Hint: Identify the invariant or transformation first; apply divisible by 4 except centuries unless divisible by 400.

Recommended approach: Apply divisible by 4 except centuries unless divisible by 400.

Complexity: O(1) time and O(1) space.

Solution explanation

Apply divisible by 4 except centuries unless divisible by 400. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

11. Factorial Iterative

Difficulty: Beginner · Category: Loops

Problem statement: Calculate n factorial for a non-negative safe integer.

Input: integer n

Output: factorial

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 5 → 120

Hint: Identify the invariant or transformation first; multiply a running result from 2 through n.

Recommended approach: Multiply a running result from 2 through n.

Complexity: O(n) time and O(1) space.

Solution explanation

Multiply a running result from 2 through n. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

12. Multiplication Table

Difficulty: Beginner · Category: Loops

Problem statement: Produce the first twelve multiples of an integer.

Input: integer n

Output: array of twelve products

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 3 → [3,6,...,36]

Hint: Identify the invariant or transformation first; loop over multipliers 1 through 12.

Recommended approach: Loop over multipliers 1 through 12.

Complexity: O(1) bounded time and O(1) bounded space.

Solution explanation

Loop over multipliers 1 through 12. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) bounded time and O(1) bounded space.

13. Sum One to N

Difficulty: Beginner · Category: Arithmetic

Problem statement: Return the sum of integers from 1 through n.

Input: non-negative integer n

Output: sum

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 5 → 15

Hint: Identify the invariant or transformation first; use the arithmetic-series formula or a loop.

Recommended approach: Use the arithmetic-series formula or a loop.

Complexity: O(1) time and O(1) space with the formula.

Solution explanation

Use the arithmetic-series formula or a loop. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space with the formula.

14. Count Digits

Difficulty: Beginner · Category: Arithmetic

Problem statement: Count decimal digits in an integer, including zero.

Input: integer n

Output: digit count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 0 → 1

Hint: Identify the invariant or transformation first; use absolute value and string length or repeated division.

Recommended approach: Use absolute value and string length or repeated division.

Complexity: O(d) time and O(d) or O(1) space.

Solution explanation

Use absolute value and string length or repeated division. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(d) time and O(d) or O(1) space.

15. Reverse Integer

Difficulty: Beginner · Category: Arithmetic

Problem statement: Reverse decimal digits while preserving the sign.

Input: integer n

Output: reversed integer

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: -120 → -21

Hint: Identify the invariant or transformation first; separate sign and reverse digits.

Recommended approach: Separate sign and reverse digits.

Complexity: O(d) time and O(d) space.

Solution explanation

Separate sign and reverse digits. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(d) time and O(d) space.

16. Digit Sum

Difficulty: Beginner · Category: Arithmetic

Problem statement: Return the sum of decimal digits in an integer.

Input: integer n

Output: digit sum

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 502 → 7

Hint: Identify the invariant or transformation first; iterate over absolute-value digits.

Recommended approach: Iterate over absolute-value digits.

Complexity: O(d) time and O(d) or O(1) space.

Solution explanation

Iterate over absolute-value digits. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(d) time and O(d) or O(1) space.

17. Palindrome Number

Difficulty: Beginner · Category: Arithmetic

Problem statement: Determine whether an integer reads the same backward.

Input: integer n

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 1221 → true

Hint: Identify the invariant or transformation first; compare with its reversed digit representation.

Recommended approach: Compare with its reversed digit representation.

Complexity: O(d) time and O(d) space.

Solution explanation

Compare with its reversed digit representation. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(d) time and O(d) space.

18. Prime Check

Difficulty: Beginner · Category: Arithmetic

Problem statement: Determine whether an integer greater than one is prime.

Input: integer n

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 29 → true

Hint: Identify the invariant or transformation first; test divisors only through the square root.

Recommended approach: Test divisors only through the square root.

Complexity: O(sqrt n) time and O(1) space.

Solution explanation

Test divisors only through the square root. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(sqrt n) time and O(1) space.

19. Primes in Range

Difficulty: Beginner · Category: Loops

Problem statement: Return all prime numbers within an inclusive range.

Input: two integers

Output: array of primes

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 10, 20 → [11,13,17,19]

Hint: Identify the invariant or transformation first; reuse a square-root prime predicate.

Recommended approach: Reuse a square-root prime predicate.

Complexity: O(r sqrt r) time and O(r) output space.

Solution explanation

Reuse a square-root prime predicate. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(r sqrt r) time and O(r) output space.

20. Greatest Common Divisor

Difficulty: Beginner · Category: Arithmetic

Problem statement: Compute the greatest common divisor of two integers.

Input: two integers

Output: gcd

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 48, 18 → 6

Hint: Identify the invariant or transformation first; use Euclid's remainder algorithm.

Recommended approach: Use Euclid's remainder algorithm.

Complexity: O(log min(a,b)) time and O(1) space.

Solution explanation

Use Euclid's remainder algorithm. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log min(a,b)) time and O(1) space.

21. Least Common Multiple

Difficulty: Beginner · Category: Arithmetic

Problem statement: Compute the least common multiple of two non-zero integers.

Input: two integers

Output: lcm

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 6, 8 → 24

Hint: Identify the invariant or transformation first; use absolute product divided by gcd.

Recommended approach: Use absolute product divided by gcd.

Complexity: O(log min(a,b)) time and O(1) space.

Solution explanation

Use absolute product divided by gcd. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log min(a,b)) time and O(1) space.

22. Celsius to Fahrenheit

Difficulty: Beginner · Category: Arithmetic

Problem statement: Convert a Celsius temperature to Fahrenheit.

Input: Celsius number

Output: Fahrenheit number

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 0 → 32

Hint: Identify the invariant or transformation first; apply F equals C times nine fifths plus 32.

Recommended approach: Apply F equals C times nine fifths plus 32.

Complexity: O(1) time and O(1) space.

Solution explanation

Apply F equals C times nine fifths plus 32. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

23. Simple Interest

Difficulty: Beginner · Category: Arithmetic

Problem statement: Calculate simple interest and final amount.

Input: principal rate years

Output: interest and total

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 1000, 5, 2 → 100 and 1100

Hint: Identify the invariant or transformation first; apply principal times rate times time.

Recommended approach: Apply principal times rate times time.

Complexity: O(1) time and O(1) space.

Solution explanation

Apply principal times rate times time. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

24. Compound Interest

Difficulty: Beginner · Category: Arithmetic

Problem statement: Calculate balance after fixed annual compounding.

Input: principal rate years

Output: final balance

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 1000, 10, 2 → 1210

Hint: Identify the invariant or transformation first; apply principal times one plus rate to the years power.

Recommended approach: Apply principal times one plus rate to the years power.

Complexity: O(1) time and O(1) space.

Solution explanation

Apply principal times one plus rate to the years power. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

25. BMI Category

Difficulty: Beginner · Category: Control Flow

Problem statement: Calculate BMI and return a standard category.

Input: weight kg and height m

Output: bmi and category

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 70, 1.75 → 22.86 normal

Hint: Identify the invariant or transformation first; validate positive inputs then use ordered thresholds.

Recommended approach: Validate positive inputs then use ordered thresholds.

Complexity: O(1) time and O(1) space.

Solution explanation

Validate positive inputs then use ordered thresholds. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

26. Grade Calculator

Difficulty: Beginner · Category: Control Flow

Problem statement: Map a numeric score to a letter grade.

Input: score from 0 to 100

Output: grade

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 87 → B

Hint: Identify the invariant or transformation first; validate range and check thresholds highest first.

Recommended approach: Validate range and check thresholds highest first.

Complexity: O(1) time and O(1) space.

Solution explanation

Validate range and check thresholds highest first. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

27. Triangle Validity

Difficulty: Beginner · Category: Control Flow

Problem statement: Determine whether three positive lengths can form a triangle.

Input: three lengths

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 3,4,5 → true

Hint: Identify the invariant or transformation first; check every pair sum exceeds the remaining side.

Recommended approach: Check every pair sum exceeds the remaining side.

Complexity: O(1) time and O(1) space.

Solution explanation

Check every pair sum exceeds the remaining side. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

28. Triangle Type

Difficulty: Beginner · Category: Control Flow

Problem statement: Classify a valid triangle as equilateral, isosceles or scalene.

Input: three lengths

Output: type

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 5,5,8 → isosceles

Hint: Identify the invariant or transformation first; validate first then compare equal sides.

Recommended approach: Validate first then compare equal sides.

Complexity: O(1) time and O(1) space.

Solution explanation

Validate first then compare equal sides. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

29. Calculator Switch

Difficulty: Beginner · Category: Control Flow

Problem statement: Apply one supported arithmetic operator to two numbers.

Input: left operator right

Output: result

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 8, *, 3 → 24

Hint: Identify the invariant or transformation first; use a switch with an explicit unsupported branch.

Recommended approach: Use a switch with an explicit unsupported branch.

Complexity: O(1) time and O(1) space.

Solution explanation

Use a switch with an explicit unsupported branch. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

30. FizzBuzz

Difficulty: Beginner · Category: Loops

Problem statement: Generate values from 1 to n replacing multiples of three and five.

Input: integer n

Output: array of labels or numbers

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 5 → [1,2,Fizz,4,Buzz]

Hint: Identify the invariant or transformation first; check divisibility by 15 before 3 or 5.

Recommended approach: Check divisibility by 15 before 3 or 5.

Complexity: O(n) time and O(n) output space.

Solution explanation

Check divisibility by 15 before 3 or 5. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.

31. First N Fibonacci

Difficulty: Beginner · Category: Loops

Problem statement: Return the first n Fibonacci numbers iteratively.

Input: non-negative integer n

Output: array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 6 → [0,1,1,2,3,5]

Hint: Identify the invariant or transformation first; maintain the previous two values.

Recommended approach: Maintain the previous two values.

Complexity: O(n) time and O(n) output space.

Solution explanation

Maintain the previous two values. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.

32. Power Without Exponent Operator

Difficulty: Beginner · Category: Loops

Problem statement: Raise a number to a non-negative integer exponent without Math.pow or exponent syntax.

Input: base and exponent

Output: power

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 3,4 → 81

Hint: Identify the invariant or transformation first; multiply repeatedly.

Recommended approach: Multiply repeatedly.

Complexity: O(exponent) time and O(1) space.

Solution explanation

Multiply repeatedly. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(exponent) time and O(1) space.

33. Character Count

Difficulty: Beginner · Category: Strings

Problem statement: Count Unicode code points in a string.

Input: string

Output: count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: A👍 → 2

Hint: Identify the invariant or transformation first; iterate with Array.from or for of.

Recommended approach: Iterate with Array.from or for of.

Complexity: O(n) time and O(n) or O(1) space.

Solution explanation

Iterate with Array.from or for of. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) or O(1) space.

34. Reverse String

Difficulty: Beginner · Category: Strings

Problem statement: Reverse a string by Unicode code points.

Input: string

Output: reversed string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: hello → olleh

Hint: Identify the invariant or transformation first; convert to code points, reverse and join.

Recommended approach: Convert to code points, reverse and join.

Complexity: O(n) time and O(n) space.

Solution explanation

Convert to code points, reverse and join. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

35. Palindrome String

Difficulty: Beginner · Category: Strings

Problem statement: Check whether normalized text is a palindrome.

Input: string

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: Never odd or even → true

Hint: Identify the invariant or transformation first; normalize case and remove non-alphanumeric characters.

Recommended approach: Normalize case and remove non-alphanumeric characters.

Complexity: O(n) time and O(n) space.

Solution explanation

Normalize case and remove non-alphanumeric characters. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

36. Count Vowels

Difficulty: Beginner · Category: Strings

Problem statement: Count English vowels in text case-insensitively.

Input: string

Output: count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: Education → 5

Hint: Identify the invariant or transformation first; scan characters and test membership in a Set.

Recommended approach: Scan characters and test membership in a Set.

Complexity: O(n) time and O(1) auxiliary space.

Solution explanation

Scan characters and test membership in a Set. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) auxiliary space.

37. Count Words

Difficulty: Beginner · Category: Strings

Problem statement: Count non-empty whitespace-separated words.

Input: string

Output: count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: one two three → 3

Hint: Identify the invariant or transformation first; trim then split on one-or-more whitespace.

Recommended approach: Trim then split on one-or-more whitespace.

Complexity: O(n) time and O(n) space.

Solution explanation

Trim then split on one-or-more whitespace. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

38. Capitalize Words

Difficulty: Beginner · Category: Strings

Problem statement: Uppercase the first code point of every word.

Input: string

Output: transformed string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: hello world → Hello World

Hint: Identify the invariant or transformation first; split words and transform each safely.

Recommended approach: Split words and transform each safely.

Complexity: O(n) time and O(n) space.

Solution explanation

Split words and transform each safely. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

39. Title Case with Small Words

Difficulty: Beginner · Category: Strings

Problem statement: Convert text to title case while keeping configured small words lowercase except at boundaries.

Input: text and small-word set

Output: title

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: the lord of the rings → The Lord of the Rings

Hint: Identify the invariant or transformation first; transform by index with a lookup Set.

Recommended approach: Transform by index with a lookup Set.

Complexity: O(n) time and O(n) space.

Solution explanation

Transform by index with a lookup Set. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

40. Remove Duplicate Characters

Difficulty: Beginner · Category: Strings

Problem statement: Keep only the first occurrence of each character.

Input: string

Output: deduplicated string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: banana → ban

Hint: Identify the invariant or transformation first; track seen characters in a Set.

Recommended approach: Track seen characters in a Set.

Complexity: O(n) time and O(k) space.

Solution explanation

Track seen characters in a Set. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.

41. First Non-Repeating Character

Difficulty: Beginner · Category: Strings

Problem statement: Return the first character that occurs once.

Input: string

Output: character or null

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: swiss → w

Hint: Identify the invariant or transformation first; build a frequency Map then scan again.

Recommended approach: Build a frequency Map then scan again.

Complexity: O(n) time and O(k) space.

Solution explanation

Build a frequency Map then scan again. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.

42. Character Frequency

Difficulty: Beginner · Category: Strings

Problem statement: Return a frequency map of characters.

Input: string

Output: Map-like counts

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: aba → {a:2,b:1}

Hint: Identify the invariant or transformation first; increment counts in a Map.

Recommended approach: Increment counts in a Map.

Complexity: O(n) time and O(k) space.

Solution explanation

Increment counts in a Map. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.

43. Anagram Check

Difficulty: Beginner · Category: Strings

Problem statement: Determine whether two normalized strings contain identical character counts.

Input: two strings

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: listen, silent → true

Hint: Identify the invariant or transformation first; normalize then compare frequency maps.

Recommended approach: Normalize then compare frequency maps.

Complexity: O(n+m) time and O(k) space.

Solution explanation

Normalize then compare frequency maps. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+m) time and O(k) space.

44. Longest Word

Difficulty: Beginner · Category: Strings

Problem statement: Return the longest whitespace-delimited word, preserving first on ties.

Input: string

Output: word

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: red green blue → green

Hint: Identify the invariant or transformation first; scan words while tracking the first maximum.

Recommended approach: Scan words while tracking the first maximum.

Complexity: O(n) time and O(w) space.

Solution explanation

Scan words while tracking the first maximum. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(w) space.

45. Truncate Text Safely

Difficulty: Beginner · Category: Strings

Problem statement: Truncate text to a maximum code-point length and append an ellipsis when needed.

Input: string and limit

Output: string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: abcdef,4 → abc…

Hint: Identify the invariant or transformation first; slice an Array of code points with room for the ellipsis.

Recommended approach: Slice an Array of code points with room for the ellipsis.

Complexity: O(n) time and O(n) space.

Solution explanation

Slice an Array of code points with room for the ellipsis. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

46. Mask Email

Difficulty: Beginner · Category: Strings

Problem statement: Mask the local part of an email while preserving domain.

Input: email string

Output: masked email

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: salman@example.com → s****n@example.com

Hint: Identify the invariant or transformation first; split at the last at-sign and preserve boundary characters.

Recommended approach: Split at the last at-sign and preserve boundary characters.

Complexity: O(n) time and O(n) space.

Solution explanation

Split at the last at-sign and preserve boundary characters. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

47. Slugify Text

Difficulty: Beginner · Category: Strings

Problem statement: Create a lowercase hyphenated ASCII slug from simple text.

Input: string

Output: slug

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: Hello, World! → hello-world

Hint: Identify the invariant or transformation first; normalize, remove punctuation, collapse separators.

Recommended approach: Normalize, remove punctuation, collapse separators.

Complexity: O(n) time and O(n) space.

Solution explanation

Normalize, remove punctuation, collapse separators. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

48. String Compression Basic

Difficulty: Beginner · Category: Strings

Problem statement: Compress consecutive repeated characters as character plus count.

Input: string

Output: compressed string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: aaabbc → a3b2c1

Hint: Identify the invariant or transformation first; run-length encode one group at a time.

Recommended approach: Run-length encode one group at a time.

Complexity: O(n) time and O(n) output space.

Solution explanation

Run-length encode one group at a time. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.

49. Expand Run-Length String

Difficulty: Beginner · Category: Strings

Problem statement: Expand a simple character-count encoding.

Input: encoded string

Output: original string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: a3b2 → aaabb

Hint: Identify the invariant or transformation first; parse a character followed by one-or-more digits.

Recommended approach: Parse a character followed by one-or-more digits.

Complexity: O(output size) time and space.

Solution explanation

Parse a character followed by one-or-more digits. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(output size) time and space.

50. Find Substring Index

Difficulty: Beginner · Category: Strings

Problem statement: Return the first index of a pattern without using indexOf.

Input: text and pattern

Output: index or -1

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: hello, ll → 2

Hint: Identify the invariant or transformation first; try each viable start and compare characters.

Recommended approach: Try each viable start and compare characters.

Complexity: O(nm) time and O(1) space.

Solution explanation

Try each viable start and compare characters. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(nm) time and O(1) space.

51. Count Substring Occurrences

Difficulty: Beginner · Category: Strings

Problem statement: Count overlapping occurrences of a substring.

Input: text and pattern

Output: count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: aaaa, aa → 3

Hint: Identify the invariant or transformation first; advance one position after each comparison.

Recommended approach: Advance one position after each comparison.

Complexity: O(nm) time and O(1) space.

Solution explanation

Advance one position after each comparison. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(nm) time and O(1) space.

52. Array Sum

Difficulty: Beginner · Category: Arrays

Problem statement: Return the sum of finite numbers in an array.

Input: number array

Output: sum

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,3] → 6

Hint: Identify the invariant or transformation first; use a loop or reduce with zero initial value.

Recommended approach: Use a loop or reduce with zero initial value.

Complexity: O(n) time and O(1) space.

Solution explanation

Use a loop or reduce with zero initial value. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

53. Array Average

Difficulty: Beginner · Category: Arrays

Problem statement: Return the arithmetic mean or null for an empty array.

Input: number array

Output: number or null

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [2,4,6] → 4

Hint: Identify the invariant or transformation first; reuse sum and divide by length after empty check.

Recommended approach: Reuse sum and divide by length after empty check.

Complexity: O(n) time and O(1) space.

Solution explanation

Reuse sum and divide by length after empty check. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

54. Array Minimum Maximum

Difficulty: Beginner · Category: Arrays

Problem statement: Return both minimum and maximum without sorting.

Input: number array

Output: min and max

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [4,1,9] → {min:1,max:9}

Hint: Identify the invariant or transformation first; track two running values.

Recommended approach: Track two running values.

Complexity: O(n) time and O(1) space.

Solution explanation

Track two running values. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

55. Remove Array Duplicates

Difficulty: Beginner · Category: Arrays

Problem statement: Return unique values while preserving first-seen order.

Input: array

Output: unique array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [2,1,2,3] → [2,1,3]

Hint: Identify the invariant or transformation first; use a Set and spread or one-pass accumulation.

Recommended approach: Use a Set and spread or one-pass accumulation.

Complexity: O(n) time and O(n) space.

Solution explanation

Use a Set and spread or one-pass accumulation. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

56. Second Largest Distinct

Difficulty: Beginner · Category: Arrays

Problem statement: Return the second largest distinct number or null.

Input: number array

Output: number or null

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [4,9,9,7] → 7

Hint: Identify the invariant or transformation first; track largest and second largest distinct values.

Recommended approach: Track largest and second largest distinct values.

Complexity: O(n) time and O(1) space.

Solution explanation

Track largest and second largest distinct values. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

57. Rotate Array Right Once

Difficulty: Beginner · Category: Arrays

Problem statement: Move the final element to the front without mutating the input.

Input: array

Output: rotated array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,3] → [3,1,2]

Hint: Identify the invariant or transformation first; slice the last value and prefix it.

Recommended approach: Slice the last value and prefix it.

Complexity: O(n) time and O(n) space.

Solution explanation

Slice the last value and prefix it. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

58. Chunk Array

Difficulty: Beginner · Category: Arrays

Problem statement: Split an array into consecutive chunks of a positive size.

Input: array and size

Output: array of arrays

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,3,4,5],2 → [[1,2],[3,4],[5]]

Hint: Identify the invariant or transformation first; advance by chunk size and slice.

Recommended approach: Advance by chunk size and slice.

Complexity: O(n) time and O(n) output space.

Solution explanation

Advance by chunk size and slice. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.

59. Flatten One Level

Difficulty: Beginner · Category: Arrays

Problem statement: Flatten exactly one nesting level without flat.

Input: array of values or arrays

Output: array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,[2,3],4] → [1,2,3,4]

Hint: Identify the invariant or transformation first; append arrays with spread and scalars directly.

Recommended approach: Append arrays with spread and scalars directly.

Complexity: O(n+k) time and O(n+k) space.

Solution explanation

Append arrays with spread and scalars directly. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+k) time and O(n+k) space.

60. Array Intersection

Difficulty: Beginner · Category: Arrays

Problem statement: Return distinct values appearing in both arrays.

Input: two arrays

Output: intersection

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,2,3],[2,3,4] → [2,3]

Hint: Identify the invariant or transformation first; use a membership Set and output Set.

Recommended approach: Use a membership Set and output Set.

Complexity: O(n+m) time and O(n+m) space.

Solution explanation

Use a membership Set and output Set. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+m) time and O(n+m) space.

61. Array Difference

Difficulty: Beginner · Category: Arrays

Problem statement: Return values from the first array absent from the second.

Input: two arrays

Output: difference

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,3],[2] → [1,3]

Hint: Identify the invariant or transformation first; build a Set from the second array.

Recommended approach: Build a Set from the second array.

Complexity: O(n+m) time and O(m) space.

Solution explanation

Build a Set from the second array. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+m) time and O(m) space.

62. Merge Sorted Arrays

Difficulty: Beginner · Category: Arrays

Problem statement: Merge two ascending numeric arrays into one ascending array.

Input: two sorted arrays

Output: sorted array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,4],[2,3] → [1,2,3,4]

Hint: Identify the invariant or transformation first; use two pointers.

Recommended approach: Use two pointers.

Complexity: O(n+m) time and O(n+m) space.

Solution explanation

Use two pointers. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+m) time and O(n+m) space.

63. Move Zeros to End

Difficulty: Beginner · Category: Arrays

Problem statement: Move numeric zeros to the end while preserving non-zero order.

Input: number array

Output: reordered array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [0,1,0,3] → [1,3,0,0]

Hint: Identify the invariant or transformation first; collect non-zeros then append zero count.

Recommended approach: Collect non-zeros then append zero count.

Complexity: O(n) time and O(n) space.

Solution explanation

Collect non-zeros then append zero count. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

64. Find Missing One to N

Difficulty: Beginner · Category: Arrays

Problem statement: Find the single missing number from an otherwise complete 1 to n sequence.

Input: array and n

Output: missing number

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,4,5],5 → 3

Hint: Identify the invariant or transformation first; subtract array sum from arithmetic-series sum.

Recommended approach: Subtract array sum from arithmetic-series sum.

Complexity: O(n) time and O(1) space.

Solution explanation

Subtract array sum from arithmetic-series sum. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

65. Pair Sum Brute to Map

Difficulty: Beginner · Category: Arrays

Problem statement: Return indices of two values that add to a target.

Input: number array and target

Output: index pair or null

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [2,7,11],9 → [0,1]

Hint: Identify the invariant or transformation first; store needed complements in a Map.

Recommended approach: Store needed complements in a Map.

Complexity: O(n) time and O(n) space.

Solution explanation

Store needed complements in a Map. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

66. Group Even and Odd

Difficulty: Beginner · Category: Arrays

Problem statement: Return separate arrays of even and odd integers.

Input: integer array

Output: object with even and odd arrays

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,2,3,4] → {even:[2,4],odd:[1,3]}

Hint: Identify the invariant or transformation first; accumulate into two buckets in one pass.

Recommended approach: Accumulate into two buckets in one pass.

Complexity: O(n) time and O(n) output space.

Solution explanation

Accumulate into two buckets in one pass. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) output space.

67. Count by Value

Difficulty: Beginner · Category: Collections

Problem statement: Return occurrence counts for arbitrary primitive values.

Input: array

Output: Map counts

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [a,b,a] → {a:2,b:1}

Hint: Identify the invariant or transformation first; use Map so keys keep their types.

Recommended approach: Use Map so keys keep their types.

Complexity: O(n) time and O(k) space.

Solution explanation

Use Map so keys keep their types. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.

68. Sort Numbers Ascending

Difficulty: Beginner · Category: Sorting

Problem statement: Return a sorted copy of a numeric array.

Input: number array

Output: ascending array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [10,2,1] → [1,2,10]

Hint: Identify the invariant or transformation first; use toSorted with a numeric comparator.

Recommended approach: Use toSorted with a numeric comparator.

Complexity: O(n log n) time and O(n) space.

Solution explanation

Use toSorted with a numeric comparator. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.

69. Sort Objects by Field

Difficulty: Beginner · Category: Sorting

Problem statement: Sort records by numeric priority without mutating input.

Input: record array

Output: sorted records

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [{p:2},{p:1}] → [{p:1},{p:2}]

Hint: Identify the invariant or transformation first; use a stable copying sort and comparator.

Recommended approach: Use a stable copying sort and comparator.

Complexity: O(n log n) time and O(n) space.

Solution explanation

Use a stable copying sort and comparator. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n log n) time and O(n) space.

70. Linear Search

Difficulty: Beginner · Category: Searching

Problem statement: Return the first index of a target using strict equality.

Input: array and target

Output: index or -1

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [a,b,c], b → 1

Hint: Identify the invariant or transformation first; scan left to right and return early.

Recommended approach: Scan left to right and return early.

Complexity: O(n) time and O(1) space.

Solution explanation

Scan left to right and return early. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

71. Binary Search Basic

Difficulty: Beginner · Category: Searching

Problem statement: Find a number in an ascending sorted array.

Input: sorted number array and target

Output: index or -1

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [1,3,5,7],5 → 2

Hint: Identify the invariant or transformation first; maintain inclusive low and high bounds.

Recommended approach: Maintain inclusive low and high bounds.

Complexity: O(log n) time and O(1) space.

Solution explanation

Maintain inclusive low and high bounds. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(log n) time and O(1) space.

72. Object Key Count

Difficulty: Beginner · Category: Objects

Problem statement: Count own enumerable string keys of an object.

Input: plain object

Output: count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {a:1,b:2} → 2

Hint: Identify the invariant or transformation first; use Object.keys length.

Recommended approach: Use Object.keys length.

Complexity: O(k) time and O(k) space.

Solution explanation

Use Object.keys length. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.

73. Invert Unique Object

Difficulty: Beginner · Category: Objects

Problem statement: Swap keys and primitive values when values are unique.

Input: plain object

Output: inverted object

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {a:x,b:y} → {x:a,y:b}

Hint: Identify the invariant or transformation first; iterate entries and assign reversed pairs.

Recommended approach: Iterate entries and assign reversed pairs.

Complexity: O(k) time and O(k) space.

Solution explanation

Iterate entries and assign reversed pairs. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.

74. Pick Object Fields

Difficulty: Beginner · Category: Objects

Problem statement: Create a new object containing only allowlisted own fields.

Input: object and key list

Output: picked object

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {a:1,b:2},[b] → {b:2}

Hint: Identify the invariant or transformation first; check Object.hasOwn for every requested key.

Recommended approach: Check Object.hasOwn for every requested key.

Complexity: O(k) time and O(k) space.

Solution explanation

Check Object.hasOwn for every requested key. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.

75. Omit Object Fields

Difficulty: Beginner · Category: Objects

Problem statement: Create a copy excluding a set of keys.

Input: object and key list

Output: filtered object

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {a:1,b:2},[a] → {b:2}

Hint: Identify the invariant or transformation first; build an exclusion Set then filter entries.

Recommended approach: Build an exclusion Set then filter entries.

Complexity: O(n+k) time and O(n) space.

Solution explanation

Build an exclusion Set then filter entries. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n+k) time and O(n) space.

76. Shallow Object Equal

Difficulty: Beginner · Category: Objects

Problem statement: Compare two plain objects by own keys and strict values.

Input: two plain objects

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {a:1},{a:1} → true

Hint: Identify the invariant or transformation first; compare key counts then every own value.

Recommended approach: Compare key counts then every own value.

Complexity: O(k) time and O(k) space.

Solution explanation

Compare key counts then every own value. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.

77. Merge Defaults

Difficulty: Beginner · Category: Objects

Problem statement: Fill missing configuration fields without overriding explicit false, zero or empty string.

Input: defaults and input

Output: merged config

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {limit:10},{limit:0} → {limit:0}

Hint: Identify the invariant or transformation first; use nullish or own-property checks, not truthiness.

Recommended approach: Use nullish or own-property checks, not truthiness.

Complexity: O(k) time and O(k) space.

Solution explanation

Use nullish or own-property checks, not truthiness. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.

78. Nested Property Getter

Difficulty: Beginner · Category: Objects

Problem statement: Read a dot-delimited own-property path safely.

Input: object and path

Output: value or undefined

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {a:{b:2}}, a.b → 2

Hint: Identify the invariant or transformation first; reduce path segments with own checks.

Recommended approach: Reduce path segments with own checks.

Complexity: O(depth) time and O(1) auxiliary space.

Solution explanation

Reduce path segments with own checks. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(depth) time and O(1) auxiliary space.

79. Array to Lookup Map

Difficulty: Beginner · Category: Data Transformation

Problem statement: Index records by unique id using a Map.

Input: record array

Output: Map by id

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [{id:7}] → Map(7 to record)

Hint: Identify the invariant or transformation first; validate IDs and insert once.

Recommended approach: Validate IDs and insert once.

Complexity: O(n) time and O(n) space.

Solution explanation

Validate IDs and insert once. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

80. Lookup Map to Array

Difficulty: Beginner · Category: Data Transformation

Problem statement: Convert a Map of records into an array of values.

Input: Map

Output: array

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: Map(a to 1,b to 2) → [1,2]

Hint: Identify the invariant or transformation first; use Array.from on values.

Recommended approach: Use Array.from on values.

Complexity: O(n) time and O(n) space.

Solution explanation

Use Array.from on values. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

81. Compose Two Functions

Difficulty: Beginner · Category: Functions

Problem statement: Return a function that applies one function then another.

Input: two unary functions

Output: composed function

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: double after addOne on 3 → 8

Hint: Identify the invariant or transformation first; return input => outer(inner(input)).

Recommended approach: Return input => outer(inner(input)).

Complexity: O(f+g) time and O(1) wrapper space.

Solution explanation

Return input => outer(inner(input)). Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(f+g) time and O(1) wrapper space.

82. Once Function

Difficulty: Beginner · Category: Functions

Problem statement: Wrap a function so it runs at most once and reuses the result.

Input: function

Output: once wrapper

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: once(counter) called twice → same first result

Hint: Identify the invariant or transformation first; close over called flag and result.

Recommended approach: Close over called flag and result.

Complexity: O(1) overhead and O(1) space.

Solution explanation

Close over called flag and result. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) overhead and O(1) space.

83. Memoize Single Argument

Difficulty: Beginner · Category: Functions

Problem statement: Cache a pure single-primitive-argument function.

Input: function

Output: memoized function

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: square(4) twice → one computation

Hint: Identify the invariant or transformation first; use a Map keyed by the argument.

Recommended approach: Use a Map keyed by the argument.

Complexity: O(1) average lookup and O(k) cache space.

Solution explanation

Use a Map keyed by the argument. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) average lookup and O(k) cache space.

84. Simple Debounce

Difficulty: Beginner · Category: Functions

Problem statement: Delay a callback until calls stop for a duration.

Input: function and delay

Output: debounced function

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: typing bursts → one final call

Hint: Identify the invariant or transformation first; reset one timeout on every call.

Recommended approach: Reset one timeout on every call.

Complexity: O(1) per call and O(1) state.

Solution explanation

Reset one timeout on every call. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per call and O(1) state.

85. Simple Throttle

Difficulty: Beginner · Category: Functions

Problem statement: Limit a callback to at most once per interval.

Input: function and interval

Output: throttled function

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: scroll burst → bounded calls

Hint: Identify the invariant or transformation first; track last execution time.

Recommended approach: Track last execution time.

Complexity: O(1) per call and O(1) state.

Solution explanation

Track last execution time. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per call and O(1) state.

86. Days Between UTC Dates

Difficulty: Beginner · Category: Dates

Problem statement: Return whole-day distance between two ISO calendar dates interpreted at UTC midnight.

Input: two YYYY-MM-DD strings

Output: day count

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 2026-01-01,2026-01-03 → 2

Hint: Identify the invariant or transformation first; parse explicit UTC timestamps and divide milliseconds.

Recommended approach: Parse explicit UTC timestamps and divide milliseconds.

Complexity: O(1) time and O(1) space.

Solution explanation

Parse explicit UTC timestamps and divide milliseconds. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

87. Format Currency

Difficulty: Beginner · Category: Internationalization

Problem statement: Format an integer minor-unit amount for a locale and currency.

Input: amount cents locale currency

Output: string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 123456,en-US,USD → $1,234.56

Hint: Identify the invariant or transformation first; use Intl.NumberFormat and divide by currency scale.

Recommended approach: Use Intl.NumberFormat and divide by currency scale.

Complexity: O(1) time and O(1) space.

Solution explanation

Use Intl.NumberFormat and divide by currency scale. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

88. Valid Simple Username

Difficulty: Beginner · Category: Regular Expressions

Problem statement: Validate a username containing letters, digits and underscores with a bounded length.

Input: string

Output: Boolean

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: salman_7 → true

Hint: Identify the invariant or transformation first; use an anchored bounded character-class pattern.

Recommended approach: Use an anchored bounded character-class pattern.

Complexity: O(n) time and O(1) space.

Solution explanation

Use an anchored bounded character-class pattern. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(1) space.

89. Extract Hashtags

Difficulty: Beginner · Category: Regular Expressions

Problem statement: Return hashtag words from text without the hash prefix.

Input: string

Output: array of tags

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: Hi #JavaScript #Web → [JavaScript,Web]

Hint: Identify the invariant or transformation first; use global Unicode-aware matching.

Recommended approach: Use global Unicode-aware matching.

Complexity: O(n) time and O(k) output space.

Solution explanation

Use global Unicode-aware matching. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) output space.

90. Parse Query String

Difficulty: Beginner · Category: Browser APIs

Problem statement: Convert a query string into an object of first values.

Input: query string

Output: plain record

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: ?page=2&q=js → {page:2,q:js}

Hint: Identify the invariant or transformation first; use URLSearchParams and explicit duplicate policy.

Recommended approach: Use URLSearchParams and explicit duplicate policy.

Complexity: O(n) time and O(k) space.

Solution explanation

Use URLSearchParams and explicit duplicate policy. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(k) space.

91. Build Query String

Difficulty: Beginner · Category: Browser APIs

Problem statement: Serialize defined record values into a query string.

Input: record

Output: query string

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: {q:js,page:2} → q=js&page=2

Hint: Identify the invariant or transformation first; append entries with URLSearchParams.

Recommended approach: Append entries with URLSearchParams.

Complexity: O(k) time and O(k) space.

Solution explanation

Append entries with URLSearchParams. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(k) time and O(k) space.

92. Local Storage JSON Wrapper

Difficulty: Beginner · Category: Browser APIs

Problem statement: Save and load JSON with parse failure handling.

Input: key and value

Output: persisted value or error

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: save settings then load → same data

Hint: Identify the invariant or transformation first; wrap stringify/parse and catch quota or syntax errors.

Recommended approach: Wrap stringify/parse and catch quota or syntax errors.

Complexity: O(n) time and O(n) space.

Solution explanation

Wrap stringify/parse and catch quota or syntax errors. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) space.

93. Create DOM List Safely

Difficulty: Beginner · Category: DOM

Problem statement: Render an array of labels into an existing list element.

Input: list element and strings

Output: updated DOM

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: [A,B] → two li children

Hint: Identify the invariant or transformation first; create elements, set textContent and replaceChildren.

Recommended approach: Create elements, set textContent and replaceChildren.

Complexity: O(n) time and O(n) DOM nodes.

Solution explanation

Create elements, set textContent and replaceChildren. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n) time and O(n) DOM nodes.

94. Toggle Button State

Difficulty: Beginner · Category: DOM

Problem statement: Toggle pressed state and label on a button click.

Input: button element

Output: synchronized aria-pressed state

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: false click → true

Hint: Identify the invariant or transformation first; read aria-pressed, invert and update text.

Recommended approach: Read aria-pressed, invert and update text.

Complexity: O(1) per event and O(1) space.

Solution explanation

Read aria-pressed, invert and update text. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) per event and O(1) space.

95. Form Required Validation

Difficulty: Beginner · Category: DOM

Problem statement: Prevent submission and show an accessible error for an empty required field.

Input: form and input

Output: valid submit or error

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: blank title → error linked to input

Hint: Identify the invariant or transformation first; listen to submit, validate trimmed value and focus invalid field.

Recommended approach: Listen to submit, validate trimmed value and focus invalid field.

Complexity: O(1) time and O(1) space.

Solution explanation

Listen to submit, validate trimmed value and focus invalid field. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) time and O(1) space.

96. Event Delegated Delete

Difficulty: Beginner · Category: DOM

Problem statement: Delete a list row when a descendant delete button is activated.

Input: container click event

Output: matching row removed

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: click data-action delete → row removed

Hint: Identify the invariant or transformation first; use closest plus containment checks.

Recommended approach: Use closest plus containment checks.

Complexity: O(depth) time and O(1) space.

Solution explanation

Use closest plus containment checks. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(depth) time and O(1) space.

97. Fetch JSON with Status Check

Difficulty: Beginner · Category: Async JavaScript

Problem statement: Fetch JSON and reject non-success HTTP responses.

Input: URL and optional signal

Output: parsed JSON

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: 200 JSON → object

Hint: Identify the invariant or transformation first; check response.ok and content type before parsing.

Recommended approach: Check response.ok and content type before parsing.

Complexity: O(payload) time and O(payload) space.

Solution explanation

Check response.ok and content type before parsing. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(payload) time and O(payload) space.

98. Sequential Delay Messages

Difficulty: Beginner · Category: Async JavaScript

Problem statement: Resolve messages after delays in sequence.

Input: message-delay pairs

Output: ordered messages

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: A 10ms, B 5ms → A then B

Hint: Identify the invariant or transformation first; await each delay in a for of loop.

Recommended approach: Await each delay in a for of loop.

Complexity: O(n plus delays) time and O(n) output space.

Solution explanation

Await each delay in a for of loop. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n plus delays) time and O(n) output space.

99. Parallel Delay Messages

Difficulty: Beginner · Category: Async JavaScript

Problem statement: Resolve independent delayed messages concurrently while preserving input order.

Input: message-delay pairs

Output: ordered messages

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: A 10ms, B 5ms → [A,B] after about 10ms

Hint: Identify the invariant or transformation first; map to Promises and await Promise.all.

Recommended approach: Map to Promises and await Promise.all.

Complexity: O(n plus max delay) time and O(n) space.

Solution explanation

Map to Promises and await Promise.all. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(n plus max delay) time and O(n) space.

100. Promise Timeout Wrapper

Difficulty: Beginner · Category: Async JavaScript

Problem statement: Reject when a Promise does not settle before a timeout.

Input: Promise and milliseconds

Output: value or timeout error

Constraints: Use modern JavaScript, validate the stated input shape, and handle empty, zero or negative values when the domain permits them.

Example: slow promise, 50ms → timeout

Hint: Identify the invariant or transformation first; race with a timer and clear it on settlement.

Recommended approach: Race with a timer and clear it on settlement.

Complexity: O(1) overhead and O(1) space.

Solution explanation

Race with a timer and clear it on settlement. Validate the documented boundary cases before the main operation, preserve the required output shape, and add tests for the example plus empty, minimum, maximum and invalid inputs. The implementation is complete when its observed time and space costs match O(1) overhead and O(1) space.