Java

Java Coding Exercises: Arrays, Strings and Collections10

Start with a calculation, then follow how the problem changes the state you need to retain. Each core exercise includes a complete Java solution, its input contract, and the reasoning behind the algorithm.

Arrays and numeric bounds

Apply signs and match indices before choosing int or long. Continue with a sequence whose products exceed int.

  1. Sum Signed Values in Java with IntStream

    Combine absolute values and matching boolean signs with IntStream. Check cancellation, all-negative input, int bounds, and the equal-length array contract.

  2. Java Array Dot Product: IntStream and Integer Bounds

    Multiply matching array elements and sum them with IntStream. Understand signs, int bounds, and why wider multiplication must promote operands before overflow.

  3. Generate an x-Spaced Sequence in Java: Promote Before Multiplication

    Build n values from x through x*n with IntStream. Explain why mapToLong alone does not prevent int overflow, and check positive, negative, and zero steps.

Strings and ordered transformations

Choose literal replacement for digit words, preserve normalization order, then use a stack for adjacent-pair removal.

  1. Convert English Digit Words in Java with String.replace

    Normalize valid mixtures of digits and zero-to-nine words using literal replacement. Distinguish replace from replaceAll and explain integer bounds and scope.

  2. Java New ID Recommendation: Normalization Order and Truncation

    Implement ID normalization with an Optional chain and a loop. Check dot collapsing, empty input after filtering, truncation cleanup, padding, and locale independence.

  3. Remove Adjacent Duplicate Pairs in Java with an ArrayDeque Stack

    Reduce adjacent equal-character pairs with a stack. Explain the prefix invariant, cascading removal, million-character boundaries, and time and space costs.

Counts, state and ordering

Distinguish name frequencies from distinct types. Then track cache recency and rank ratios with an explicit tie-break.

  1. Find the Missing Runner in Java with Name Frequencies

    Subtract completion counts from participant counts with HashMap. Handle duplicate names without Set subtraction and explain the one-missing-runner contract.

  2. Java Ponketmon: Why the Answer Is min(Distinct Types, N/2)

    Count distinct types with HashSet and prove the selection bound is achievable. Distinguish the maximum type count from constructing an actual selection.

  3. Java LRU Cache: Use Access Order in LinkedHashMap

    Simulate an LRU cache with access-ordered LinkedHashMap. Check hit refresh, eviction, case-insensitive keys, zero capacity, and the model’s cost assumptions.

  4. Sort Failure Rates in Java: Denominators, Ties, and Long Products

    Count stopped and reached players, sort exact rates with long cross-products, and handle unreachable stages, full completion, and stage-number tie-breaking.