Programming Puzzle Solutions: JS, Java, Python, Go
This is the index for the four programming puzzle problems on this site. Each one has language-specific solutions in JavaScript, Java, Python, or Go — pick the variant that matches what you’re working in. Every solution is tested, runs in linear time, and includes the trade-off between the idiomatic approach and the manual one.
Find the number with the most digits
Given an array of positive integers, return the one with the most digits. If two have the same digit count, return the first occurrence. The trick is choosing between String(n).length (idiomatic, slightly slower) and Math.log10(n) + 1 (mathematical, faster on hot paths).
- Find Number With Most Digits in JavaScript — three solutions: reduce, manual loop, and
Math.log10. Benchmarks at 1k / 100k / 10M elements. - Find Number With Most Digits in Java — stream API vs. for-loop. Both O(n).
- Find Number With Most Digits in Go — slice traversal with
strconv.Itoavs.math.Log10.
Permutations of a word, alphabetically ordered
Generate every permutation of a string with no built-in functions. Sort the output alphabetically. The constraint forces a backtracking implementation rather than itertools.permutations. Watch the duplicate-character handling — the standard naive recursion produces duplicates for repeated letters.
- Permutations Alphabetically in JavaScript — recursive backtracking, dedup via
Set, then sort. - Permutations Alphabetically in Java — same algorithm,
TreeSetto keep alphabetical order without a separate sort step.
Longest vowel substring
Find the longest contiguous substring containing only vowels (aeiou). Linear-scan with a sliding window. The edge cases are the all-consonants string (return empty) and tied lengths (return the first).
- Longest Vowel Substring in Python — sliding window,
O(n)time,O(1)space. - Longest Vowel Substring in JavaScript — same algorithm, JS-idiomatic with a
Setfor vowel lookup.
Longest alphabetical substring
Find the longest substring where each character is alphabetically ≥ the previous. Linear-scan with reset on break. The edge case is a single-character input (always length 1) and a fully-descending string (also length 1, return the first character).
- Longest Alphabetical Substring in Java —
charcomparison, indexes only. - Longest Alphabetical Substring in JavaScript —
charCodeAtcomparison, no string slicing in the loop.
Why these problems
These four problems show up frequently in junior-engineer screening interviews, particularly for FAANG-tier and FAANG-adjacent roles. They test:
- Linear-time thinking. All four are solvable in
O(n)once you stop reaching for sort or nested loops. - Edge-case discipline. Ties, empty input, single-char input, descending input — every solution on this site handles them explicitly.
- Language idiom familiarity. The same algorithm in JavaScript, Java, Python, and Go reveals where each language helps and hurts.
If you’re prepping for technical screens, work through all four in your weakest language first. The repetition across solutions makes the language idiom stick faster than four unrelated problems would.
Related
- Determine the Highest-Scoring Word in Java — similar linear-scan + max-tracking pattern.
- Find the Unique Number Using Java — XOR trick, also
O(n). - How to Solve Finding Neo in Java — 2D grid traversal.