Dataset Viewer
Auto-converted to Parquet Duplicate
id
stringlengths
16
16
difficulty
stringclasses
3 values
category
stringclasses
11 values
problem
stringclasses
25 values
signature
stringclasses
25 values
reasoning
stringclasses
25 values
code
stringclasses
25 values
tests
stringlengths
106
815
verified
bool
1 class
language
stringclasses
1 value
code_hash
stringclasses
25 values
sft_text
stringclasses
25 values
meta
stringlengths
11
47
vcr_b9814d68bcf9
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [1, 39, 5, 1, 30, 30, 57, 39, 11, 5, 18, 11, 35, 35, 18]}, "output": 57}, {"input": {"nums": [51, 26, 4, 4, 8, 20, 20, 26, 8]}, "output": 51}, {"input": {"nums": [7, 1, 7, 1, 4, 19, 44, 19, 4]}, "output": 44}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "757184104daef17b"}
vcr_430948dcc240
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [41, 19, 9, 21, 19, 7, 7, 51, 5, 11]}, "output": 44}, {"input": {"prices": [29, 4, 27, 35, 16, 40, 27, 14, 27, 38, 32]}, "output": 36}, {"input": {"prices": [20, 5, 30, 21, 14, 37, 6, 38, 10]}, "output": 33}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "d673d0b0ab301f5f"}
vcr_d217e8f95a3c
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [35, -30, 52, 62, 31, 4, -35, 68, 72, 59, 32], "target": 5}, "output": [0, 1]}, {"input": {"nums": [-10, 28, 19, 47, 12, -8, -14, 49, 24, 5, -17], "target": 47}, "output": [1, 2]}, {"input": {"nums": [17, -20, -19, -16], "target": -36}, "output": [1, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "2dbbfc77d94d8f53"}
vcr_389f5e248cc7
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 5, 6, 9, 11, 25, 30, 31, 32, 38, 40, 41, 46, 47], "target": 21}, "output": 5}, {"input": {"nums": [5, 11, 16, 18, 19, 20, 30, 33, 34], "target": 3}, "output": 0}, {"input": {"nums": [1, 14, 28, 33, 36, 38], "target": 34}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "fc4d497398abaaae"}
vcr_62e1fa6bc405
easy
bit
Return the number of set bits in non-negative integer n (Hamming weight).
def hamming_weight(n: int) -> int:
Count the least bit with n & 1, then shift right until n is 0.
def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c
[{"input": {"n": 6872}, "output": 7}, {"input": {"n": 12340}, "output": 5}, {"input": {"n": 6119}, "output": 10}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "d720b67a93c9a6cc"}
vcr_c28820a3ad90
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 1, 2, 3, 7, 8, 8, 9, 9, 10, 12, 13]}, "output": [1, 2, 3, 7, 8, 9, 10, 12, 13]}, {"input": {"nums": [1, 3, 3, 10]}, "output": [1, 3, 10]}, {"input": {"nums": [3, 5, 8, 9, 10, 10, 10, 10]}, "output": [3, 5, 8, 9, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "ee7acfdf6b3a85dd"}
vcr_38d1c4660c2a
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 2, 3, 3, 6, 6]}, "output": [1, 2, 3, 6]}, {"input": {"nums": [3, 4, 4, 5, 10]}, "output": [3, 4, 5, 10]}, {"input": {"nums": [1, 3, 3, 4, 5, 5, 5, 7, 8, 11, 11, 12]}, "output": [1, 3, 4, 5, 7, 8, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "a9fd4af8a10c20c0"}
vcr_149cdcaf1bd3
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [56, 15, 60, 9, 18, 22]}, "output": 45}, {"input": {"prices": [17, 15, 2, 40, 23, 3, 23, 28, 16, 18, 3, 38]}, "output": 38}, {"input": {"prices": [40, 39, 22, 15, 17, 25, 3]}, "output": 10}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "4ca31ff10fc70c0d"}
vcr_2ba78ece4f4b
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [5, 3, 6, 6, 3, 5]}, "output": [1620, 2700, 1350, 1350, 2700, 1620]}, {"input": {"nums": [4, 3, 3, 4, 4]}, "output": [144, 192, 192, 144, 144]}, {"input": {"nums": [5, 4, 2, 2, 1, 2]}, "output": [32, 40, 80, 80, 160, 80]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "9157e4df3178326a"}
vcr_c46892bdb771
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 1, 4, 1, 4, 1, 2, 1, 1, 0]}, "output": true}, {"input": {"nums": [3, 1, 3, 3, 0, 3, 0, 3, 0]}, "output": true}, {"input": {"nums": [3, 2, 1]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "2c18d758d89aac51"}
vcr_03c69aa078b3
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [7, 12, 11, 21]}, "output": [7, 19, 30, 51]}, {"input": {"nums": [-3, 0, 5]}, "output": [-3, -3, 2]}, {"input": {"nums": [-5, -1, 10, 7]}, "output": [-5, -6, 4, 11]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "0f0e702959163bea"}
vcr_7a327971ddf6
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [16, 13, 19]}, "output": 35}, {"input": {"nums": [20, 15, 11, 1, 4, 7, 15, 12]}, "output": 50}, {"input": {"nums": [14, 18, 19, 5, 4, 9, 20, 4, 2]}, "output": 59}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "ce0f7da139d5131a"}
vcr_36bcdb3d0a1f
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "eb edb aeda"}, "output": "aeda edb eb"}, {"input": {"s": "bb aaa bab aabb bbba"}, "output": "bbba aabb bab aaa bb"}, {"input": {"s": "aab aaa aaab bb"}, "output": "bb aaab aaa aab"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "09eb2042dcffadee"}
vcr_1e15ab3cbc25
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-21, -18, 31, -25, -16, 20]}, "output": [-21, -39, -8, -33, -49, -29]}, {"input": {"nums": [13, 2, 10, 3, -3, 2, -6]}, "output": [13, 15, 25, 28, 25, 27, 21]}, {"input": {"nums": [2, -12, 8, 14]}, "output": [2, -10, -2, 12]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "fdd8ebe2a4f582e9"}
vcr_c13f6e8dedef
hard
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "cbdabbadbc"}, "output": true}, {"input": {"s": "bbc"}, "output": false}, {"input": {"s": "aab"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "380d852d4af89752"}
vcr_b15a75397ef4
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [12, 35, 35, 28, 53, 12, 28]}, "output": 53}, {"input": {"nums": [20, 19, 20, 13, 11, 19, 8, 15, 15, 49, 8, 13, 11]}, "output": 49}, {"input": {"nums": [21, 59, 7, 25, 7, 14, 25, 24, 24, 14, 21]}, "output": 59}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "17c1568a3e66ab83"}
vcr_7f2ecb3096e7
easy
bit
Return the number of set bits in non-negative integer n (Hamming weight).
def hamming_weight(n: int) -> int:
Count the least bit with n & 1, then shift right until n is 0.
def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c
[{"input": {"n": 32962}, "output": 4}, {"input": {"n": 6777}, "output": 8}, {"input": {"n": 14846}, "output": 11}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "e4813d1e8201a7c2"}
vcr_7f918aee975c
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [24, 9, -12, -7, -14, 25, -8, 21, 18, -15, 23, -2, 22]}, "output": [24, 33, 21, 14, 0, 25, 17, 38, 56, 41, 64, 62, 84]}, {"input": {"nums": [10, -10, -8, 16]}, "output": [10, 0, -8, 8]}, {"input": {"nums": [1, -6, 11, 17, -6, 1]}, "output": [1, -5, 6, 23, 17, 18]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "21f1a78ead32ca57"}
vcr_1f6dc4cff8a6
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [3, 14, 18, 19, 12, -14, 0, 12, 12, 6]}, "output": 82}, {"input": {"nums": [-4, -12, -7, 12, 14, 6, -11]}, "output": 32}, {"input": {"nums": [-9, 9, -1, -8, 9, 3]}, "output": 12}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "910e2449eae86734"}
vcr_4316d1abf3bf
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [19, 7, 4, 10, 23]}, "output": 46}, {"input": {"nums": [12, 4, 20, 17, 13, 1, 10, 5, 1, 16]}, "output": 71}, {"input": {"nums": [11, 7, 5, 5]}, "output": 16}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "a7736370534938ab"}
vcr_e37ba6e2709a
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [6, 8, 11, 20, 21, 22, 27, 31, 35], "target": 49}, "output": 9}, {"input": {"nums": [12, 15, 16, 25, 33, 39], "target": 36}, "output": 5}, {"input": {"nums": [11, 13, 18, 28, 30, 32, 33, 34, 39], "target": 25}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "9c913f59b1278b3b"}
vcr_c1fa6def9067
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 6, 16, 18, 25, 28, 31, 32, 34, 38, 43, 47, 49], "target": 48}, "output": 12}, {"input": {"nums": [6, 11, 14, 16, 21, 22, 27], "target": 5}, "output": 0}, {"input": {"nums": [1, 2, 4, 6, 7, 12, 21, 24, 37], "target": 11}, "output": 5}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "4c6c2b24003674aa"}
vcr_dc8c016e9c1e
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [39, 3, 30, 39, 16, 3, 16, 30, 2, 76, 2]}, "output": 76}, {"input": {"nums": [29, 2, 21, 2, 29, 40, 21]}, "output": 40}, {"input": {"nums": [25, 50, 27, 1, 27, 29, 8, 25, 21, 29, 21, 8, 1]}, "output": 50}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6a7154e9086f5846"}
vcr_3c6f5e51702f
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 4, 5, 8, 8]}, "output": [1, 4, 5, 8]}, {"input": {"nums": [1, 3, 3, 4, 4, 4, 6, 6, 6, 6, 6, 9]}, "output": [1, 3, 4, 6, 9]}, {"input": {"nums": [0, 0, 0, 7, 8, 9, 11, 11, 11, 11, 12]}, "output": [0, 7, 8, 9, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "b53dc702734138e2"}
vcr_bf59f2fae845
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [3, 1, 11, 25]}, "output": 28}, {"input": {"nums": [18, 16, 1, 19, 16, 17, 14, 5, 17]}, "output": 71}, {"input": {"nums": [19, 11, 17, 9, 8, 5, 13, 0]}, "output": 57}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "cdc8e11cc8ebe031"}
vcr_657df15f931c
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [3, 10, 12, 14, 18, 40, 44, 47], "target": 1}, "output": 0}, {"input": {"nums": [13, 15, 18, 23, 26, 27, 28, 30], "target": 6}, "output": 0}, {"input": {"nums": [0, 6, 11, 20], "target": 5}, "output": 1}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "01829787d2b68617"}
vcr_8a6efeaf3f10
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [17, 23, 39, 50, 6, 1, 24, 17]}, "output": true}, {"input": {"nums": [4, 14, 9, 23, 16, 0, 16]}, "output": true}, {"input": {"nums": [12, 23, 12]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "e53d5a3398571f09"}
vcr_ed27c3c0dc43
hard
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [23, 43, 55, 63, -10, 54], "target": 45}, "output": [2, 4]}, {"input": {"nums": [-17, -29, -18, -4], "target": -21}, "output": [0, 3]}, {"input": {"nums": [5, 48, 11, -10, 20, 7, -16], "target": 55}, "output": [1, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "f278e41f482cf9f4"}
vcr_bf42dac38d1b
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [17, -2, 5, -8, -9], "k": 1}, "output": [-9, 17, -2, 5, -8]}, {"input": {"nums": [-7, -10, -2, -1], "k": 7}, "output": [-10, -2, -1, -7]}, {"input": {"nums": [10, -8, -9, -5, 4, 7, 3, -6, -4], "k": 9}, "output": [10, -8, -9, -5, 4, 7, 3, -6, -4]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "365e74978aecc354"}
vcr_72502a5f0337
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [36, -22, -34, -21, -37], "target": -56}, "output": [1, 2]}, {"input": {"nums": [11, 47, 17, 23, -25, -11, 45, -18, 21], "target": 3}, "output": [7, 8]}, {"input": {"nums": [17, 49, 36, 21, -2, -30, -22, -13, 6], "target": 15}, "output": [0, 4]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "54d49dc689770f40"}
vcr_b7234f5216a5
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "dd edccbb e d"}, "output": "d e edccbb dd"}, {"input": {"s": "abbb bbaa baba"}, "output": "baba bbaa abbb"}, {"input": {"s": "a aa abba aaa aa"}, "output": "aa aaa abba aa a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "ac2ef3b62ceae9ba"}
vcr_7f873aea2920
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 34, "b": 74}, "output": 2}, {"input": {"a": 102, "b": 164}, "output": 2}, {"input": {"a": 155, "b": 79}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "533cc76af5a0b61c"}
vcr_f1cfcc2d3a29
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 202, "b": 173}, "output": 1}, {"input": {"a": 55, "b": 129}, "output": 1}, {"input": {"a": 46, "b": 47}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "dd509ab4c50116ec"}
vcr_a73548fd8b1e
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-29, -24, -8, -6, 25, 35], "target": 27}, "output": [2, 5]}, {"input": {"nums": [-19, 14, 16, 21, 22, 25], "target": 47}, "output": [4, 5]}, {"input": {"nums": [-16, -9, 5, 6, 7, 12, 21, 33], "target": -25}, "output": [0, 1]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "6cd721ec513a5623"}
vcr_2c5185c1e09c
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "dedbdd a abeaae eba abddca adccc dcb"}, "output": "dcb adccc abddca eba abeaae a dedbdd"}, {"input": {"s": "aaa baba abab"}, "output": "abab baba aaa"}, {"input": {"s": "a baa b ab baaa"}, "output": "baaa ab b baa a"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "a44f98a5185a5645"}
vcr_ec3efac00af1
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-13, -2, 1, 13, 25, 34, 42, 43], "target": 14}, "output": [2, 3]}, {"input": {"nums": [-7, 11, 12, 24, 32], "target": 56}, "output": [3, 4]}, {"input": {"nums": [-10, -3, -2, -1, 0, 15, 26, 37], "target": 27}, "output": [0, 7]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "d84ca4600887ad87"}
vcr_d8815aaeb8c4
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [36, 36, 16, 37, 35, 35, 37, 73, 16]}, "output": 73}, {"input": {"nums": [5, 16, 5, 6, 2, 20, 2, 19, 16, 48, 19, 6, 20]}, "output": 48}, {"input": {"nums": [16, 4, 47, 12, 4, 16, 12]}, "output": 47}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "27f08aaf0766a63f"}
vcr_9d5c044c415b
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [1, 14, 17, 27, 30, 31, 37, 41, 45, 47, 48], "target": 0}, "output": 0}, {"input": {"nums": [2, 6, 16, 22, 26, 28, 34], "target": 20}, "output": 3}, {"input": {"nums": [4, 8, 11, 18, 19, 26, 30, 31, 33], "target": 12}, "output": 3}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "f76a7f5bed5481dc"}
vcr_582597051867
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [18, 5, 1, 14, 2, 14, 29]}, "output": 61}, {"input": {"nums": [4, 5, 1, 5, 10, 8, 17]}, "output": 32}, {"input": {"nums": [6, 4, 6, 7]}, "output": 13}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "7c387e8ad3f960c6"}
vcr_0a6850c9e558
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [52, 40, 30, 67, -29, 44, 4, 8, 55, -15], "target": 11}, "output": [1, 4]}, {"input": {"nums": [32, -15, -23, 25, 38, -5, -27, 11, -11], "target": 33}, "output": [4, 5]}, {"input": {"nums": [46, 33, -23, 28, 37, 21, -29, -10, 43], "target": 17}, "output": [0, 6]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "8ab2edfc47871a70"}
vcr_309914cdff3a
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [23, 23, 2, 12, 16]}, "output": 41}, {"input": {"nums": [11, 5, 8, 3]}, "output": 19}, {"input": {"nums": [15, 15, 6, 10]}, "output": 25}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "33dc684e7868a512"}
vcr_8616e0ffbfd4
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-28, -23, -18, -13, -12, 14, 22, 24, 46], "target": 18}, "output": [0, 8]}, {"input": {"nums": [-17, -12, -6, -4, -2, 10, 15, 33], "target": -19}, "output": [0, 4]}, {"input": {"nums": [8, 24, 30, 38], "target": 62}, "output": [1, 3]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "128fe31f135aa073"}
vcr_a858ea9bf36a
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [43, 15, 22, 12, 31, 26, 43]}, "output": true}, {"input": {"nums": [4, 24, 2]}, "output": false}, {"input": {"nums": [17, 11, 23, 11, 25, 0, 24, 2]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "97d1e51770f1aaf6"}
vcr_35c612652e67
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [5, 7, 8, 14, 20, 23, 26, 32, 41, 47, 48], "target": 12}, "output": 3}, {"input": {"nums": [0, 10, 13, 14, 22, 26, 27, 28, 32, 34, 35], "target": 34}, "output": 9}, {"input": {"nums": [2, 4, 10, 13, 14, 17, 21, 26, 34, 38, 39], "target": 14}, "output": 4}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "04560be787b38be6"}
vcr_d8dbbfae600c
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["baa", "bc", "abc", "bbcb", "bbb", "bbca"]}, "output": [1, 1, 1, 1, 1, 1]}, {"input": {"strs": ["aab", "baa", "aab", "aaa", "bab"]}, "output": [1, 1, 3]}, {"input": {"strs": ["bbb", "bba", "bbb", "bba", "bbb"]}, "output": [2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "c0f07c7d8978ed67"}
vcr_b232beac91bb
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [23, 36, 39, 39, 75, 28, 14, 14, 23, 16, 28, 16, 36]}, "output": 75}, {"input": {"nums": [14, 3, 18, 17, 26, 18, 26, 13, 3, 13, 46, 14, 17]}, "output": 46}, {"input": {"nums": [25, 12, 58, 25, 15, 15, 12]}, "output": 58}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "976e4b8333e10ff0"}
vcr_9f0fbbd4b4ce
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 0, 3, 3, 2, 0, 0]}, "output": true}, {"input": {"nums": [3, 3, 0, 3, 2, 0, 3, 0, 1]}, "output": true}, {"input": {"nums": [3, 3, 3, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "3723f0614fb50094"}
vcr_8654e4fd4beb
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 2, 8, 9, 11, 13]}, "output": [0, 2, 8, 9, 11, 13]}, {"input": {"nums": [0, 1, 3, 3, 4, 4, 6, 7, 10, 10, 11]}, "output": [0, 1, 3, 4, 6, 7, 10, 11]}, {"input": {"nums": [2, 4, 5, 6, 6, 7, 8, 8, 11, 12]}, "output": [2, 4, 5, 6, 7, 8, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "8b9877785af4d549"}
vcr_78e779deb016
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [20, 17, 17, 20, -16, 10], "k": 16}, "output": [17, 20, -16, 10, 20, 17]}, {"input": {"nums": [-3, 7, 0, 2, -6, 3, 4, 0], "k": 19}, "output": [3, 4, 0, -3, 7, 0, 2, -6]}, {"input": {"nums": [1, 2, 8], "k": 12}, "output": [1, 2, 8]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "67c1f7340a95589f"}
vcr_24f8c3319c57
easy
string
Reverse the order of words in s (words separated by single spaces).
def reverse_words(s: str) -> str:
Split on whitespace, reverse the list, join with spaces.
def reverse_words(s: str) -> str: return ' '.join(reversed(s.split()))
[{"input": {"s": "ec babab baeb"}, "output": "baeb babab ec"}, {"input": {"s": "abbb b a bb b"}, "output": "b bb a b abbb"}, {"input": {"s": "bba aa bab aaba ba"}, "output": "ba aaba bab aa bba"}]
true
python
e27e5a0fb61b865a
### Problem Reverse the order of words in s (words separated by single spaces). ### Reasoning Split on whitespace, reverse the list, join with spaces. ### Solution ```python def reverse_words(s: str) -> str: return ' '.join(reversed(s.split())) ```
{"v": "ok", "content_hash": "3be1ebeaa687f86d"}
vcr_103be990be97
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [-5, -1, 15, -17, -12, 11, -17], "k": 10}, "output": [-12, 11, -17, -5, -1, 15, -17]}, {"input": {"nums": [-6, 4, 8, -5], "k": 5}, "output": [-5, -6, 4, 8]}, {"input": {"nums": [2, -6, 3], "k": 5}, "output": [-6, 3, 2]}]
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "ad2a49a1e1bb53a7"}
vcr_765e3207305d
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [45, 45, 42, 38, 24, 7, 20, 44, 49, 50, 30, 5, 19, 27, 53, 6]}, "output": 48}, {"input": {"prices": [11, 17, 7, 18, 39, 24, 22]}, "output": 32}, {"input": {"prices": [21, 16, 19, 3, 40]}, "output": 37}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "34568da993275b38"}
vcr_2326f275e124
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [11, 30, 28, 6, 20, 29, 17, 3]}, "output": 76}, {"input": {"nums": [16, 6, 0, 0, 17, 5, 1, 0, 2]}, "output": 36}, {"input": {"nums": [0, 5, 16, 2, 1, 4, 20, 10]}, "output": 37}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "5c26572848bb190a"}
vcr_3cb8076c174b
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [3, 4, 3, 3, 4, 0, 1, 0]}, "output": true}, {"input": {"nums": [3, 2, 1, 3, 0, 2, 0, 0]}, "output": true}, {"input": {"nums": [2, 1, 0, 3, 3, 2, 0, 0, 2]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "07f9b6b3f4e59ebd"}
vcr_cd3464a85b0b
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [21, -14, 17, -26, 38, 69, 44, 52, -13, 55, 10, -25], "target": 107}, "output": [4, 5]}, {"input": {"nums": [-8, -27, -2, 39, 34, -14, 48, 23], "target": 32}, "output": [2, 4]}, {"input": {"nums": [-4, 41, -19, -23, 29, 23, 46], "target": 64}, "output": [1, 5]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "4e991d7a2f047845"}
vcr_78ce92c926ac
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [5, 3, 30, 20, 30, 30, 13, 3, 28]}, "output": 106}, {"input": {"nums": [0, 13, 7, 12, 1, 20]}, "output": 45}, {"input": {"nums": [11, 7, 8, 3, 3]}, "output": 22}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "17807b450d425ddf"}
vcr_1eed07596a7e
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [15, 37, 19, 34, 44, 33, 50, 11, 15]}, "output": true}, {"input": {"nums": [6, 13, 10, 15, 20]}, "output": false}, {"input": {"nums": [8, 16, 9, 10, 14, 19, 14, 4]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "b518a0347c34588e"}
vcr_fb1bb8b709b6
easy
array
Rotate nums to the right by k steps (k may be larger than n). Return a new list.
def rotate(nums: list[int], k: int) -> list[int]:
Reduce k modulo n. Last k elements move to the front.
def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return nums[-k:] + nums[:-k]
[{"input": {"nums": [16, 13, 11, 5, -1, -6, 16, 0, 11, 0, 8, 11], "k": 14}, "output": [8, 11, 16, 13, 11, 5, -1, -6, 16, 0, 11, 0]}, {"input": {"nums": [-10, -4, -5, -3, 2, 4, 0, -3, 0, -7], "k": 9}, "output": [-4, -5, -3, 2, 4, 0, -3, 0, -7, -10]}, {"input": {"nums": [-6, 6, -3, 9, -2], "k": 9}, "output": [6, -3, 9, -...
true
python
132c4800a39464fd
### Problem Rotate nums to the right by k steps (k may be larger than n). Return a new list. ### Reasoning Reduce k modulo n. Last k elements move to the front. ### Solution ```python def rotate(nums: list[int], k: int) -> list[int]: n = len(nums) k %= n if k == 0: return list(nums) return num...
{"v": "ok", "content_hash": "21f35d38f6c71a78"}
vcr_6572df05964e
easy
math
Return the greatest common divisor of positive integers a and b.
def gcd(a: int, b: int) -> int:
Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0.
def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a
[{"input": {"a": 142, "b": 65}, "output": 1}, {"input": {"a": 123, "b": 99}, "output": 3}, {"input": {"a": 120, "b": 43}, "output": 1}]
true
python
0a8801b251631b1d
### Problem Return the greatest common divisor of positive integers a and b. ### Reasoning Euclidean algorithm: replace (a, b) with (b, a % b) until b is 0. ### Solution ```python def gcd(a: int, b: int) -> int: while b: a, b = b, a % b return a ```
{"v": "ok", "content_hash": "2e897d37cfab1188"}
vcr_f207c432fc31
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [23, 8, 23, 20, 13, 26, 28, 11, 24, 28, 12]}, "output": 123}, {"input": {"nums": [11, 4, 14, 5, 4]}, "output": 29}, {"input": {"nums": [6, 18, 0, 6]}, "output": 24}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "dc387de1a319a58f"}
vcr_61c58422eb85
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "cbccbababcbaa"}, "output": 11}, {"input": {"s": "bacbbaba"}, "output": 7}, {"input": {"s": "bbabacacacaa"}, "output": 10}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "4f85e1547df528e7"}
vcr_acaa6ef1f881
easy
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [33, 3, 35, 8, 13, 46, 79, -5], "target": 48}, "output": [2, 4]}, {"input": {"nums": [10, 43, 9, -20], "target": -10}, "output": [0, 3]}, {"input": {"nums": [40, -17, 28, 31, -9], "target": 71}, "output": [0, 3]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "c27df79909486e93"}
vcr_3f2f6606f85f
medium
two_pointers
Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers.
def two_sum_sorted(nums: list[int], target: int) -> list[int]:
Start at both ends. Move the pointer that brings the sum closer to target.
def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < j: s = nums[i] + nums[j] if s == target: return [i, j] if s < target: i += 1 else: j -= 1 return []
[{"input": {"nums": [-11, -3, -2, 9, 18, 36], "target": -13}, "output": [0, 2]}, {"input": {"nums": [-3, -2, 2, 8, 10, 19], "target": 7}, "output": [0, 4]}, {"input": {"nums": [-19, -14, -12, -8, 5, 6, 16, 18, 21, 22], "target": 26}, "output": [4, 8]}]
true
python
0fdf37b1be7420d0
### Problem Sorted nums: find indices i < j with nums[i] + nums[j] = target using O(n) two pointers. ### Reasoning Start at both ends. Move the pointer that brings the sum closer to target. ### Solution ```python def two_sum_sorted(nums: list[int], target: int) -> list[int]: i, j = 0, len(nums) - 1 while i < ...
{"v": "ok", "content_hash": "e4e17b31e4025659"}
vcr_f4bb0ee4ed87
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [12, -17, 12, 11, -6, 22, 3, -1, 2, -1, -6, -10, -6, -1]}, "output": 43}, {"input": {"nums": [-4, -11, 3]}, "output": 3}, {"input": {"nums": [-12, 0, 13, -2]}, "output": 13}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "921df569ba3303c4"}
vcr_f3dfe3a8f826
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 1, 7, 5, 6, 5, 5]}, "output": [5250, 10500, 1500, 2100, 1750, 2100, 2100]}, {"input": {"nums": [5, 2, 4]}, "output": [8, 20, 10]}, {"input": {"nums": [5, 3, 2]}, "output": [6, 10, 15]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "ea3e5abf911bec72"}
vcr_f046516bf3de
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 3, 6, 11, 15, 26, 35, 37, 39, 43], "target": 6}, "output": 2}, {"input": {"nums": [9, 13, 14, 20, 23, 28, 36], "target": 36}, "output": 6}, {"input": {"nums": [7, 8, 34], "target": 22}, "output": 2}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "c1370fd866a42c07"}
vcr_6e4485a85069
easy
two_pointers
Return True if lowercase string s is a palindrome.
def is_palindrome(s: str) -> bool:
Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space.
def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += 1 j -= 1 return True
[{"input": {"s": "abcadbbadba"}, "output": false}, {"input": {"s": "bccbac"}, "output": false}, {"input": {"s": "bbacbbcbc"}, "output": false}]
true
python
9742b2c402b3548d
### Problem Return True if lowercase string s is a palindrome. ### Reasoning Two pointers from both ends. Any mismatch means False. O(n) time, O(1) space. ### Solution ```python def is_palindrome(s: str) -> bool: i, j = 0, len(s) - 1 while i < j: if s[i] != s[j]: return False i += ...
{"v": "ok", "content_hash": "5653c39f6ac3d37d"}
vcr_19c4dd41e1b8
easy
string
Count contiguous groups of identical characters (example: aabbc -> 3).
def count_char_groups(s: str) -> int:
Walk the string and increment whenever the character changes.
def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[i - 1]: groups += 1 return groups
[{"input": {"s": "babcaccbabbb"}, "output": 9}, {"input": {"s": "caab"}, "output": 3}, {"input": {"s": "ccccacb"}, "output": 4}]
true
python
b9d6cf6fe0c38dc7
### Problem Count contiguous groups of identical characters (example: aabbc -> 3). ### Reasoning Walk the string and increment whenever the character changes. ### Solution ```python def count_char_groups(s: str) -> int: if not s: return 0 groups = 1 for i in range(1, len(s)): if s[i] != s[...
{"v": "ok", "content_hash": "faf8fadab9264939"}
vcr_df2e4e58f072
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [3, 4, 10, 12, 12, 13, 13, 14, 15, 15]}, "output": [3, 4, 10, 12, 13, 14, 15]}, {"input": {"nums": [1, 3, 3, 4, 4, 9]}, "output": [1, 3, 4, 9]}, {"input": {"nums": [0, 1, 3, 4, 6, 7, 7, 7, 8, 9, 11]}, "output": [0, 1, 3, 4, 6, 7, 8, 9, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "7428e8f56fd54aa1"}
vcr_f1caca338b8f
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [1, 10, 27, 24, 17, 7, 12, 19]}, "output": 64}, {"input": {"nums": [2, 8, 1, 14, 5, 17]}, "output": 39}, {"input": {"nums": [9, 20, 8, 15, 2, 3, 18]}, "output": 53}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "2321220ac9c864e0"}
vcr_26863c87f348
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [9, 13, -12, 29, 1, 8]}, "output": [9, 22, 10, 39, 40, 48]}, {"input": {"nums": [-3, 14, -8, -7]}, "output": [-3, 11, 3, -4]}, {"input": {"nums": [-4, -5, -12]}, "output": [-4, -9, -21]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "f2a6bab68c6fc5c8"}
vcr_63f056af5e9a
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 4, 1, 0, 3, 3, 4, 2, 0]}, "output": true}, {"input": {"nums": [1, 1, 1, 3, 0, 2, 3, 3, 0]}, "output": true}, {"input": {"nums": [3, 1, 2, 1, 3, 3, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "dbec3de9d3658977"}
vcr_14c0accd8d13
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 7, 7, 11, 14]}, "output": [0, 7, 11, 14]}, {"input": {"nums": [0, 1, 3, 11]}, "output": [0, 1, 3, 11]}, {"input": {"nums": [1, 3, 4, 5, 5, 6, 6, 7, 8]}, "output": [1, 3, 4, 5, 6, 7, 8]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "405aa812a62ebb24"}
vcr_bd534f6fa39c
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [25, -25, -25, -23, 33, 17, 16, 35, -19]}, "output": [25, 0, -25, -48, -15, 2, 18, 53, 34]}, {"input": {"nums": [9, 3, 8]}, "output": [9, 12, 20]}, {"input": {"nums": [10, 13, -10, 1, -10]}, "output": [10, 23, 13, 14, 4]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "4e8b5d42e8daa3aa"}
vcr_80141b281ecd
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [13, -5, -24]}, "output": [13, 8, -16]}, {"input": {"nums": [-11, -5, 8, -12]}, "output": [-11, -16, -8, -20]}, {"input": {"nums": [2, 15, 10]}, "output": [2, 17, 27]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "039b4c966d142d34"}
vcr_4fe2da230a2f
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [1, 2, 3, 4, 5, 8, 9, 11, 12, 14, 14, 14, 15]}, "output": [1, 2, 3, 4, 5, 8, 9, 11, 12, 14, 15]}, {"input": {"nums": [0, 0, 1, 2, 6, 7, 8, 8, 11, 12]}, "output": [0, 1, 2, 6, 7, 8, 11, 12]}, {"input": {"nums": [0, 2, 4, 4, 6, 7, 8, 10, 10, 11, 12]}, "output": [0, 2, 4, 6, 7, 8, 10, 11, 12]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "12e009121184867e"}
vcr_40d8eabfbf65
easy
greedy
Daily stock prices. Max profit from one buy and one later sell (0 if impossible).
def max_profit(prices: list[int]) -> int:
Track the minimum price so far. Maximize price minus that minimum.
def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, x - lo) lo = min(lo, x) return best
[{"input": {"prices": [5, 19, 50, 25, 18, 29, 48, 45, 60, 42]}, "output": 55}, {"input": {"prices": [34, 3, 33, 34]}, "output": 31}, {"input": {"prices": [22, 16, 13, 19, 12, 1, 36, 31, 2]}, "output": 35}]
true
python
b40e7d74850552a7
### Problem Daily stock prices. Max profit from one buy and one later sell (0 if impossible). ### Reasoning Track the minimum price so far. Maximize price minus that minimum. ### Solution ```python def max_profit(prices: list[int]) -> int: best, lo = 0, prices[0] for x in prices[1:]: best = max(best, ...
{"v": "ok", "content_hash": "e71e99e7b99d04a3"}
vcr_fba7cc86d279
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [5, 34, 39, 25, 30, 48, 5]}, "output": true}, {"input": {"nums": [13, 13, 11, 20, 22, 7]}, "output": true}, {"input": {"nums": [24, 23, 19, 7, 25, 3, 13, 12, 25, 16, 5, 12]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "f148c4c9cb939129"}
vcr_b47f291cb354
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [0, 0, 1, 2, 6, 12]}, "output": [0, 1, 2, 6, 12]}, {"input": {"nums": [0, 0, 1, 3, 6, 7]}, "output": [0, 1, 3, 6, 7]}, {"input": {"nums": [3, 3, 4, 4, 5, 5, 8, 8, 11]}, "output": [3, 4, 5, 8, 11]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "86463b56b89779a1"}
vcr_7bb731680d25
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [28, 14, 5, 26, 22, 4, 29, 0, 14, 20, 30]}, "output": 128}, {"input": {"nums": [4, 20, 19]}, "output": 23}, {"input": {"nums": [15, 18, 15, 13, 19, 14, 1, 1]}, "output": 50}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "2d7a6e84312d7c31"}
vcr_4388486a7a53
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["cb", "bc", "bacc", "abca"]}, "output": [1, 1, 2]}, {"input": {"strs": ["baa", "bbb", "abb", "aba", "bbb", "aaa", "baa"]}, "output": [1, 1, 2, 3]}, {"input": {"strs": ["abb", "aab", "bbb", "bba", "bab", "bbb"]}, "output": [1, 2, 3]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "96cb267b4b3055c4"}
vcr_71fb0b349882
medium
dp
Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount.
def rob(nums: list[int]) -> int:
DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space.
def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, prev2 + x) return prev1
[{"input": {"nums": [13, 29, 3, 21, 6, 11, 1, 21, 17, 12, 30, 29]}, "output": 123}, {"input": {"nums": [18, 1, 2, 7, 12, 7, 13, 16, 16]}, "output": 61}, {"input": {"nums": [12, 7, 10, 18, 3, 9, 14]}, "output": 44}]
true
python
adc43e90c6cebcb1
### Problem Houses in a line with values nums. Cannot rob adjacent houses. Maximum amount. ### Reasoning DP: for each house choose max(skip, take + prev2). O(n) time, O(1) space. ### Solution ```python def rob(nums: list[int]) -> int: prev2 = prev1 = 0 for x in nums: prev2, prev1 = prev1, max(prev1, p...
{"v": "ok", "content_hash": "276b6436df50f62d"}
vcr_e01b00e845da
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [6, -14, 11, 5, 16, 21, -11, 25, -7, -7]}, "output": [6, -8, 3, 8, 24, 45, 34, 59, 52, 45]}, {"input": {"nums": [-9, 18]}, "output": [-9, 9]}, {"input": {"nums": [-10, 8, 1, -11, 12, 12, 11]}, "output": [-10, -2, -1, -12, 0, 12, 23]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "a1bfafb9c10b7283"}
vcr_2ebee8e4ff9a
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [1, 1, 2, 0, 1, 2]}, "output": [0, 0, 0, 4, 0, 0]}, {"input": {"nums": [3, 2, 5]}, "output": [10, 15, 6]}, {"input": {"nums": [4, 3, 2, 2, 4, 4]}, "output": [192, 256, 384, 384, 192, 192]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "e1e7e64d13718053"}
vcr_79630b9f3a56
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [18, -10, 18, -18, 16, 10, -11, -7, -8, -13, 3, 2]}, "output": 34}, {"input": {"nums": [10, 9, -7, 6, -2, -2, -4, 2, -1]}, "output": 19}, {"input": {"nums": [-12, -10, -5, 13, 13, 0, 3, 8]}, "output": 37}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "32a34cd71f691a04"}
vcr_2b55352b8fcb
medium
array
out[i] is product of all elements except nums[i]. O(n) without division.
def product_except_self(nums: list[int]) -> list[int]:
Build prefix products, then multiply by suffix products in a reverse pass.
def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in range(n): out[i] = left left *= nums[i] right = 1 for i in range(n - 1, -1, -1): out[i] *= right right *= nums[i] return out
[{"input": {"nums": [2, 5, 2, 5, 6, 7, 6, 4]}, "output": [50400, 20160, 50400, 20160, 16800, 14400, 16800, 25200]}, {"input": {"nums": [3, 3, 2, 3, 3]}, "output": [54, 54, 81, 54, 54]}, {"input": {"nums": [3, 1, 4, 5]}, "output": [20, 60, 15, 12]}]
true
python
e6962ae39100e8b1
### Problem out[i] is product of all elements except nums[i]. O(n) without division. ### Reasoning Build prefix products, then multiply by suffix products in a reverse pass. ### Solution ```python def product_except_self(nums: list[int]) -> list[int]: n = len(nums) out = [1] * n left = 1 for i in rang...
{"v": "ok", "content_hash": "cce2740b6835aed0"}
vcr_54965deb9ce0
medium
array
Maximum sum of any non-empty contiguous subarray (Kadane, O(n)).
def max_subarray(nums: list[int]) -> int:
At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays.
def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) best = max(best, cur) return best
[{"input": {"nums": [7, 2, -11, -5, -13, 17, -4]}, "output": 17}, {"input": {"nums": [3, -8, -9, 8, 1, 9, 13, 6, -7]}, "output": 37}, {"input": {"nums": [14, 2, -1, -2, 14]}, "output": 27}]
true
python
ec0ac96e944ed958
### Problem Maximum sum of any non-empty contiguous subarray (Kadane, O(n)). ### Reasoning At each step cur = max(x, cur+x). Track global best. Handles all-negative arrays. ### Solution ```python def max_subarray(nums: list[int]) -> int: best = cur = nums[0] for x in nums[1:]: cur = max(x, cur + x) ...
{"v": "ok", "content_hash": "ce51c342f470b0e3"}
vcr_fd127658a962
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [4, 16, 4, 32, 16, 32, 50]}, "output": 50}, {"input": {"nums": [45, 14, 14, 2, 2]}, "output": 45}, {"input": {"nums": [9, 4, 53, 4, 9]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6e3aa34066008fe6"}
vcr_22ea227f4753
medium
hashmap
Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists.
def two_sum(nums: list[int], target: int) -> list[int]:
1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space O(n).
def two_sum(nums: list[int], target: int) -> list[int]: seen = {} for i, x in enumerate(nums): need = target - x if need in seen: a, b = seen[need], i return [a, b] if a < b else [b, a] seen[x] = i return []
[{"input": {"nums": [23, 4, -3, 55, 40, -15], "target": 1}, "output": [1, 2]}, {"input": {"nums": [-4, -22, 48, -19, -15, -11, 8], "target": 37}, "output": [2, 5]}, {"input": {"nums": [48, 21, -27, 40, 47, 42, 28, 17, -14, 49, 15], "target": 45}, "output": [6, 7]}]
true
python
98882f4a63a35075
### Problem Return ascending indices of the two numbers in nums that add up to target. Exactly one solution exists. ### Reasoning 1) Find i, j with nums[i] + nums[j] = target. 2) Store value to index in a hash map while scanning. 3) For each x, look up target - x; if present, return sorted indices. 4) Time O(n), space...
{"v": "ok", "content_hash": "6528857429419044"}
vcr_ae1977dc2563
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [68, 108, 99, 61, 156]}, "output": false}, {"input": {"nums": [3, 21, 7, 11, 9, 10, 11, 21, 11, 7]}, "output": true}, {"input": {"nums": [6, 15, 22, 3, 17, 25, 18, 2, 8]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "5fa08f2e235af9ef"}
vcr_4e7783d50095
medium
hashmap
Group anagrams by sorted character signature. Return sorted group sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]:
Signature is sorted characters. Bucket words, then emit sorted sizes.
def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) for w in strs: g[''.join(sorted(w))].append(w) return sorted(len(v) for v in g.values())
[{"input": {"strs": ["bba", "ba", "bc", "bbab"]}, "output": [1, 1, 1, 1]}, {"input": {"strs": ["bbb", "bab", "bab"]}, "output": [1, 2]}, {"input": {"strs": ["bbb", "bab", "aab", "bba", "aab", "abb", "abb"]}, "output": [1, 2, 4]}]
true
python
98cc167af301acd0
### Problem Group anagrams by sorted character signature. Return sorted group sizes. ### Reasoning Signature is sorted characters. Bucket words, then emit sorted sizes. ### Solution ```python def anagram_group_sizes(strs: list[str]) -> list[int]: from collections import defaultdict g = defaultdict(list) f...
{"v": "ok", "content_hash": "a14a0ebd8489bda1"}
vcr_fdbf859e1be4
medium
binary_search
Sorted distinct nums. Return index of target, or the insertion index if missing.
def search_insert(nums: list[int], target: int) -> int:
Binary search for the lower bound of target.
def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nums[mid] < target: lo = mid + 1 else: hi = mid return lo
[{"input": {"nums": [2, 13, 15, 24, 26, 30, 38, 41, 48], "target": 35}, "output": 6}, {"input": {"nums": [6, 12, 13, 14, 19, 20, 21, 26, 38, 39], "target": 38}, "output": 8}, {"input": {"nums": [8, 13, 15, 20, 32], "target": 3}, "output": 0}]
true
python
97339c7a2a724cba
### Problem Sorted distinct nums. Return index of target, or the insertion index if missing. ### Reasoning Binary search for the lower bound of target. ### Solution ```python def search_insert(nums: list[int], target: int) -> int: lo, hi = 0, len(nums) while lo < hi: mid = (lo + hi) // 2 if nu...
{"v": "ok", "content_hash": "c6f44421756f19b9"}
vcr_218d05add289
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [0, 92, 71, 157, 185, 177, 21, 22, 142]}, "output": false}, {"input": {"nums": [8, 1, 16, 6, 15, 14, 15, 24]}, "output": true}, {"input": {"nums": [25, 13, 12, 21, 5, 4, 6, 17, 10, 20, 18, 20]}, "output": true}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "a97071ddb712cae2"}
vcr_c45cfb8c8e1c
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 2, 0, 4, 0, 0, 1, 0]}, "output": true}, {"input": {"nums": [2, 2, 2, 1, 2, 1, 2, 0]}, "output": true}, {"input": {"nums": [1, 2, 3]}, "output": true}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "03bc5f7e9caeab27"}
vcr_ab2e5fa3ef55
easy
hashmap
Return True if any value appears at least twice in nums.
def contains_duplicate(nums: list[int]) -> bool:
Duplicates exist if and only if len(list) > len(set). O(n).
def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums))
[{"input": {"nums": [147, 135, 127, 155, 87, 164]}, "output": false}, {"input": {"nums": [9, 3, 9, 11, 10]}, "output": true}, {"input": {"nums": [4, 12, 17, 22, 8]}, "output": false}]
true
python
f35d656879667375
### Problem Return True if any value appears at least twice in nums. ### Reasoning Duplicates exist if and only if len(list) > len(set). O(n). ### Solution ```python def contains_duplicate(nums: list[int]) -> bool: return len(nums) != len(set(nums)) ```
{"v": "ok", "content_hash": "658ff25e97a836a2"}
vcr_9909551c4f9e
easy
bit
Every element appears twice except one. Find the single element using XOR.
def single_number(nums: list[int]) -> int:
XOR cancels pairs. The remaining value is the single number.
def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x
[{"input": {"nums": [33, 36, 27, 37, 10, 33, 5, 63, 10, 5, 36, 27, 37, 23, 18, 23, 18]}, "output": 63}, {"input": {"nums": [9, 26, 9, 13, 13, 26, 42]}, "output": 42}, {"input": {"nums": [53, 4, 14, 26, 17, 26, 14, 17, 4]}, "output": 53}]
true
python
f76c1da3c46d529e
### Problem Every element appears twice except one. Find the single element using XOR. ### Reasoning XOR cancels pairs. The remaining value is the single number. ### Solution ```python def single_number(nums: list[int]) -> int: x = 0 for v in nums: x ^= v return x ```
{"v": "ok", "content_hash": "6f3cfec01c3e381d"}
vcr_9a7a048b18d2
easy
two_pointers
Sorted nums may contain duplicates. Return unique values in order as a new list.
def remove_duplicates_sorted(nums: list[int]) -> list[int]:
Scan left to right. Append only when the value differs from the last kept value.
def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [nums[0]] for x in nums[1:]: if x != out[-1]: out.append(x) return out
[{"input": {"nums": [7, 7, 9, 12, 13]}, "output": [7, 9, 12, 13]}, {"input": {"nums": [3, 3, 5, 8, 9]}, "output": [3, 5, 8, 9]}, {"input": {"nums": [1, 3, 3, 4, 5, 6, 10]}, "output": [1, 3, 4, 5, 6, 10]}]
true
python
9a3cff9b9f3d1889
### Problem Sorted nums may contain duplicates. Return unique values in order as a new list. ### Reasoning Scan left to right. Append only when the value differs from the last kept value. ### Solution ```python def remove_duplicates_sorted(nums: list[int]) -> list[int]: if not nums: return [] out = [n...
{"v": "ok", "content_hash": "28564d75aaafe235"}
vcr_c7100ba10e3c
easy
bit
Return the number of set bits in non-negative integer n (Hamming weight).
def hamming_weight(n: int) -> int:
Count the least bit with n & 1, then shift right until n is 0.
def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c
[{"input": {"n": 13182}, "output": 10}, {"input": {"n": 3299}, "output": 7}, {"input": {"n": 13299}, "output": 10}]
true
python
24a3439f03888452
### Problem Return the number of set bits in non-negative integer n (Hamming weight). ### Reasoning Count the least bit with n & 1, then shift right until n is 0. ### Solution ```python def hamming_weight(n: int) -> int: c = 0 while n: c += n & 1 n >>= 1 return c ```
{"v": "ok", "content_hash": "3ae5d43c384b8005"}
vcr_520ffd5fc403
medium
greedy
From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable.
def can_jump(nums: list[int]) -> bool:
Track the farthest reachable index. If the current index exceeds it, fail.
def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > reach: return False reach = max(reach, i + x) if reach >= len(nums) - 1: return True return reach >= len(nums) - 1
[{"input": {"nums": [4, 3, 4, 2, 2, 3, 1, 1, 1, 0]}, "output": true}, {"input": {"nums": [0, 0, 3, 2, 0, 2, 0]}, "output": false}, {"input": {"nums": [0, 0, 0, 1, 3, 3, 2, 1]}, "output": false}]
true
python
b6a822bf8a6dd0fa
### Problem From index 0 you may jump at most nums[i] steps. Return True if the last index is reachable. ### Reasoning Track the farthest reachable index. If the current index exceeds it, fail. ### Solution ```python def can_jump(nums: list[int]) -> bool: reach = 0 for i, x in enumerate(nums): if i > ...
{"v": "ok", "content_hash": "4c7c86a2b5c06f12"}
vcr_34cee52be086
easy
array
Return running sum where out[i] = nums[0] + ... + nums[i].
def running_sum(nums: list[int]) -> list[int]:
Accumulate once left to right. O(n).
def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out
[{"input": {"nums": [-8, 6, -10, 29, -20, -8, 23, -4]}, "output": [-8, -2, -12, 17, -3, -11, 12, 8]}, {"input": {"nums": [-1, -9, -10, 2, 14, 1, 8, 15, 1]}, "output": [-1, -10, -20, -18, -4, -3, 5, 20, 21]}, {"input": {"nums": [12, 14, 12, 2, 7, 2, 5]}, "output": [12, 26, 38, 40, 47, 49, 54]}]
true
python
9222adfde6ffe86d
### Problem Return running sum where out[i] = nums[0] + ... + nums[i]. ### Reasoning Accumulate once left to right. O(n). ### Solution ```python def running_sum(nums: list[int]) -> list[int]: acc, out = 0, [] for x in nums: acc += x out.append(acc) return out ```
{"v": "ok", "content_hash": "d159be8864804da4"}
End of preview. Expand in Data Studio

Verifiable Code Reasoning

Execution-verified Python problems with chain-of-thought

Sandbox-checked solutions · Multi-test unit checks · Deduplicated instances · Training-ready sft_text

License: MIT Examples


Overview

Verifiable Code Reasoning is a large-scale dataset of Python coding problems where every kept solution has passed sandboxed unit tests.

Unlike scraped contest dumps or unverified LLM traces, an example enters this release only if:

  1. a reference implementation exists,
  2. it runs under a time-limited sandbox, and
  3. it matches expected outputs on at least 3 tests.

Failed executions are discarded. Near-duplicate instances are filtered by content fingerprint (signature + tests), so shared solution templates across different inputs are allowed, but repeated test instances are not.

Property Detail
Verification Multiprocess sandbox, timeout, ≥3 tests
Dedup Unique id + instance content hash (signature + tests)
Balance Category & difficulty quotas during generation
Resume Hub checkpoints: train.jsonl, progress.json
License MIT

Current release size: 1,500,000 verified examples (generation target 1,500,000).


Dataset statistics

Difficulty

Difficulty Count
easy 783,088
medium 698,789
hard 18,123

Category

Category Count
array 313,191
hashmap 240,984
two_pointers 218,129
greedy 144,377
binary_search 134,400
dp 134,400
string 133,673
bit 132,948
math 46,941
stack 702
matrix 255

Schema

Field Type Description
id string Stable example id
problem string Natural-language problem statement
signature string Python function signature
reasoning string Numbered chain-of-thought
code string Reference Python solution
tests string JSON list of {"input": ..., "output": ...} (≥3)
category string array, hashmap, dp, binary_search, ...
difficulty string easy / medium / hard
sft_text string Ready-to-train prompt block
verified bool Always true in this release
code_hash string Hash of solution source (audit)
language string python

tests is stored as a JSON string so Arrow/HF can keep one schema while outputs may be int, bool, or list.


Quick start

from datasets import load_dataset
import json

ds = load_dataset("smshahbaj/verifiable-code-reasoning")
row = ds["train"][0]
print(row["sft_text"][:500])
tests = json.loads(row["tests"])
print(tests[0])

SFT format (sft_text)

### Problem
...

### Reasoning
1) ...
2) ...

### Solution
```python
...

---

## How it was built

1. **25** algorithmic generator families (arrays, strings, math, DP, greedy, binary search, bits, matrices, …)
2. Each example gets **≥3** unit tests (including varied inputs)
3. Solution is executed in a **sandbox**; mismatch / timeout / exception → drop
4. **Quotas** limit over-representation of a single category or difficulty
5. **Instance dedup** on signature+tests fingerprint (unrepeatable test cases)
6. Multi-session scale via Hugging Face checkpoints every 2000 new verifies

---

## Intended uses

- Supervised fine-tuning on **code + reasoning**
- Process / outcome supervision signals (execution as ground truth)
- Filtering or ranking other synthetic coding traces

## Out of scope / limitations

- Coverage is bounded by the **generator bank** (not full competitive-programming breadth)
- **Python only** in this release
- Synthetic style can still be distributionally narrow — always evaluate models on real benchmarks (HumanEval, MBPP, LiveCodeBench, etc.)
- Not a substitute for human-written contest editorials

---

## Citation

```bibtex
@misc{verifiable_code_reasoning,
  title        = {Verifiable Code Reasoning},
  author       = {smshahbaj},
  year         = {2026},
  howpublished = {\url{https://huggingface.co/datasets/smshahbaj/verifiable-code-reasoning}},
  note         = {Execution-verified Python coding CoT dataset}
}

Verified or it does not ship.

Downloads last month
768

Models trained or fine-tuned on smshahbaj/verifiable-code-reasoning