Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Example 1:
Input: nums = [0,1,1,0]
Output: [0,1]
Explanation:
The numbers 0 and 1 each appear twice in the array.
Example 2:
Input: nums = [0,3,2,1,3,2]
Output: [2,3]
Explanation:
The numbers 2 and 3 each appear twice in the array.
Example 3:
Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2]
Output: [4,5]
Explanation:
The numbers 4 and 5 each appear twice in the array.
Constraints:
2 <= n <= 100nums.length == n + 20 <= nums[i] < nnums contains exactly two repeated elements.Problem summary: In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual. As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[0,1,1,0]
[0,3,2,1,3,2]
[7,1,5,4,3,4,6,0,9,5,8,2]
find-all-duplicates-in-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3289: The Two Sneaky Numbers of Digitville
class Solution {
public int[] getSneakyNumbers(int[] nums) {
int[] ans = new int[2];
int[] cnt = new int[100];
int k = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans[k++] = x;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3289: The Two Sneaky Numbers of Digitville
func getSneakyNumbers(nums []int) (ans []int) {
cnt := [100]int{}
for _, x := range nums {
cnt[x]++
if cnt[x] == 2 {
ans = append(ans, x)
}
}
return
}
# Accepted solution for LeetCode #3289: The Two Sneaky Numbers of Digitville
class Solution:
def getSneakyNumbers(self, nums: List[int]) -> List[int]:
cnt = Counter(nums)
return [x for x, v in cnt.items() if v == 2]
// Accepted solution for LeetCode #3289: The Two Sneaky Numbers of Digitville
use std::collections::HashMap;
impl Solution {
pub fn get_sneaky_numbers(nums: Vec<i32>) -> Vec<i32> {
let mut cnt = HashMap::new();
for x in nums {
*cnt.entry(x).or_insert(0) += 1;
}
let mut ans = Vec::new();
for (x, v) in cnt {
if v == 2 {
ans.push(x);
}
}
ans
}
}
// Accepted solution for LeetCode #3289: The Two Sneaky Numbers of Digitville
function getSneakyNumbers(nums: number[]): number[] {
const ans: number[] = [];
const cnt: number[] = Array(100).fill(0);
for (const x of nums) {
if (++cnt[x] > 1) {
ans.push(x);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.