id
stringlengths
21
21
messages
listlengths
2
2
meta
dict
codehard-train-000000
[ { "role": "user", "content": "**Laske sanajaot**\n\nPalauta, kuinka monella eri tavalla `s` voidaan jakaa listan `words` sanoiksi (jokaista sanaa saa käyttää kuinka monesti tahansa), modulo 1 000 000 007. Tyhjällä jonolla on täsmälleen yksi jako. `s` voi olla kymmeniä tuhansia merkkejä pitkä.\n\nAllekirjoit...
{ "kind": "code.hard.word_break.count", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "count_segmentations", "problem_id": "661c665d1c61", "complexity": "O(n · L) set lookups (L = longest word length), O(n) space", "mode": "solve", "checked_by": "refer...
codehard-train-000001
[ { "role": "user", "content": "**Fewest points that cover every interval**\n\nYou are given intervals `[start, end]` with `start < end`. Return the smallest number of points such that every interval contains at least one of them (endpoints included, so the point 2 covers both `[1, 2]` and `[2, 3]`). There ca...
{ "kind": "code.hard.interval_greedy.points", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_points_to_cover", "problem_id": "cdf4939fe2d3", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "explain", "checked_by": "reference == br...
codehard-train-000002
[ { "role": "user", "content": "**Fewest intervals to remove**\n\nYou are given intervals `[start, end]` with `start < end`. Return the smallest number of intervals to remove so that the rest do not overlap. Intervals that only touch (`[1, 2]` and `[2, 3]`) do not overlap. There can be 300,000 intervals.\n\nS...
{ "kind": "code.hard.interval_greedy.remove", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_removals", "problem_id": "80778b127a4e", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "explain", "checked_by": "reference == brute-for...
codehard-train-000003
[ { "role": "user", "content": "**Word break**\n\nReturn True if `s` can be split into a sequence of one or more words from `words` (each word can be used any number of times), and False otherwise. The empty string counts as splittable. `s` can be tens of thousands of characters long.\n\nSignature:\n```python...
{ "kind": "code.hard.word_break.can", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "word_break", "problem_id": "b1a741a936bf", "complexity": "O(n · L) set lookups (L = longest word length), O(n) space", "mode": "tests", "checked_by": "reference == bru...
codehard-train-000004
[ { "role": "user", "content": "**LRU cache**\n\nImplement `RecentCache(capacity)` with `get(key)` (the value, or -1 if absent) and `put(key, value)`. Both count as a use of the key. When a put would exceed the capacity, evict the least recently used key first. Both operations must be O(1) on average.\n\nSign...
{ "kind": "code.hard.lru_cache.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "RecentCache", "problem_id": "e9eeef7af2fc", "complexity": "O(1) average per operation, O(capacity) space", "mode": "solve", "checked_by": "reference == brute-force or...
codehard-train-000005
[ { "role": "user", "content": "**Välisummat päivityksillä**\n\nToteuta `RangeSum(nums)`, jossa `update(index, value)` asettaa `nums[index] = value` ja `query(left, right)` palauttaa summan `nums[left..right]` (molemmat päät mukaan lukien, `left <= right`). Arvoja voi olla 100 000 ja operaatioita 150 000, jot...
{ "kind": "code.hard.range_sum.class1", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "RangeSum", "problem_id": "b62ae4719863", "complexity": "O(n) to build, O(log n) per operation, O(n) space", "mode": "tests", "checked_by": "reference == brute-force ...
codehard-train-000006
[ { "role": "user", "content": "**Reppuongelma (rajoittamaton)**\n\nEsine i painaa `weights[i]` ja on arvoltaan `values[i]` (kaikki positiivisia kokonaislukuja). Palauta suurin kokonaisarvo, jonka kokonaispaino on enintään `capacity`; jokaista esinettä voi ottaa kuinka monta kertaa tahansa.\n\nAllekirjoitus:\...
{ "kind": "code.hard.knapsack.unbounded", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "best_load", "problem_id": "bbd681649271", "complexity": "O(n · capacity) time, O(capacity) space", "mode": "solve", "checked_by": "reference == brute-force oracle ...
codehard-train-000007
[ { "role": "user", "content": "**Longest common substring**\n\nReturn the length of the longest string that appears as a contiguous substring in both `a` and `b`. Both strings can be over a thousand characters long.\n\nSignature:\n```python\ndef longest_common_substring(a: str, b: str) -> int:\n```\n\nExampl...
{ "kind": "code.hard.common_sequence.substring", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_common_substring", "problem_id": "92f6b9711b3e", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "explain", "checked_by": "referen...
codehard-train-000008
[ { "role": "user", "content": "**Count big inversions**\n\nReturn the number of index pairs `i < j` with `a[i] > 2 * a[j]`. Values can be negative and the list can have 150,000 elements, so checking every pair is too slow.\n\nSignature:\n```python\ndef reverse_pairs(a: list[int]) -> int:\n```\n\nExamples:\n`...
{ "kind": "code.hard.inversions.double", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "reverse_pairs", "problem_id": "ee3155a925ea", "complexity": "O(n log n) time, O(n) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 21 inp...
codehard-train-000009
[ { "role": "user", "content": "**Pienin laivan kapasiteetti**\n\nPaketit painoilla `weights` on kuljetettava annetussa järjestyksessä `days` päivässä (1 <= days <= len(weights)). Joka päivä laiva ottaa peräkkäisiä paketteja, joiden yhteispaino ei ylitä sen kapasiteettia. Palauta pienin kapasiteetti, jolla tä...
{ "kind": "code.hard.search_answer.ship", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_ship_capacity", "problem_id": "a38c7a51eb24", "complexity": "O(n log R) time (R = size of the answer range), O(1) space", "mode": "tests", "checked_by": "refer...
codehard-train-000010
[ { "role": "user", "content": "**Longest common substring**\n\nReturn the length of the longest string that appears as a contiguous substring in both `a` and `b`. Both strings can be over a thousand characters long.\n\nSignature:\n```python\ndef longest_common_substring(a: str, b: str) -> int:\n```\n\nExampl...
{ "kind": "code.hard.common_sequence.substring", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_common_substring", "problem_id": "92f6b9711b3e", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "tests", "checked_by": "reference...
codehard-train-000011
[ { "role": "user", "content": "**Eniten päällekkäisyyksiä välttäviä välejä**\n\nSaat välit `[start, end]`, joissa `start < end`. Palauta suurin määrä välejä, jotka voi pitää niin, ettei mitkään kaksi mene päällekkäin. Välit, jotka vain koskettavat (`[1, 2]` ja `[2, 3]`), eivät mene päällekkäin. Välejä voi ol...
{ "kind": "code.hard.interval_greedy.keep", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "most_meetings", "problem_id": "647ae8077147", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "explain", "checked_by": "reference == brute-forc...
codehard-train-000012
[ { "role": "user", "content": "**Histogrammin suurin neliö**\n\n`heights[i]` on histogrammin pylvään `i` korkeus; jokainen pylväs on 1 leveä, ja pylväät ovat vierekkäin. Palauta suurimman pylväiden sisään mahtuvan akselien suuntaisen neliön pinta-ala. Pylväitä voi olla satoja tuhansia.\n\nAllekirjoitus:\n```...
{ "kind": "code.hard.histogram.square0", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "max_square_area", "problem_id": "d87fffcb5cc7", "complexity": "O(n) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force oracle on 21 input...
codehard-train-000013
[ { "role": "user", "content": "**Count primes in a range**\n\nReturn how many prime numbers `p` satisfy `lo <= p <= hi`. `lo` can be negative and `lo > hi` means an empty range. `hi` can be up to 8,000,000.\n\nSignature:\n```python\ndef primes_between(lo: int, hi: int) -> int:\n```\n\nExamples:\n```python\n>...
{ "kind": "code.hard.primes_range.sieve", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "primes_between", "problem_id": "fcfb2ec4ac3a", "complexity": "O(hi log log hi) time, O(hi) space", "mode": "explain", "checked_by": "reference == brute-force oracl...
codehard-train-000014
[ { "role": "user", "content": "**First edge that closes a cycle**\n\nThere are `n` nodes numbered 0..n-1 and a list of undirected edges `[u, v]`. Edges are added one by one in the given order. Return the first edge `[u, v]` whose two ends are already connected when it is added (a self-loop counts), or `[]` i...
{ "kind": "code.hard.union_find.first_cycle", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "first_cycle_edge", "problem_id": "68a840458140", "complexity": "O((n + m) α(n)) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force o...
codehard-train-000015
[ { "role": "user", "content": "**Longest substring with at most k kinds of characters**\n\nReturn the length of the longest substring of `s` that contains at most `k` distinct characters (`k >= 0`). `s` can be hundreds of thousands of characters long.\n\nSignature:\n```python\ndef longest_k_distinct(s: str, ...
{ "kind": "code.hard.longest_window.kdistinct", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_k_distinct", "problem_id": "6c59d711db68", "complexity": "O(n) time, O(k) space", "mode": "solve", "checked_by": "reference == brute-force oracle on ...
codehard-train-000016
[ { "role": "user", "content": "**Cheapest route to a target**\n\nA directed graph has `n` nodes (0..n-1) and edges `[u, v, w]` from `u` to `v` with weight `w >= 1`. Return the length of the shortest path from `src` to `dst`, or -1 if `dst` cannot be reached. There can be 100,000 nodes and 300,000 edges.\n\nS...
{ "kind": "code.hard.dijkstra.target", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "fastest_route", "problem_id": "a629b677aea7", "complexity": "O((n + m) log m) time, O(n + m) space", "mode": "solve", "checked_by": "reference == brute-force oracle o...
codehard-train-000017
[ { "role": "user", "content": "**Sliding-window rate limiter**\n\nImplement `SlidingWindowLimiter(limit, window)` with `allow(t)`. A call at time `t` is allowed if fewer than `limit` allowed calls happened in the window `(t - window, t]`; allowed calls are recorded, rejected ones are not. Times never decreas...
{ "kind": "code.hard.rate_limiter.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "SlidingWindowLimiter", "problem_id": "60e4efc3c05f", "complexity": "amortised O(1) per call, O(limit) space", "mode": "explain", "checked_by": "reference == brute-...
codehard-train-000018
[ { "role": "user", "content": "**Balanced brackets**\n\nReturn True if every `(`, `[` and `{` in `s` is closed by the matching bracket in the right order, and False otherwise. Other characters are ignored.\n\nSignature:\n```python\ndef is_balanced(s: str) -> bool:\n```\n\nExamples:\n```python\n>>> is_balance...
{ "kind": "code.hard.brackets.valid", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "is_balanced", "problem_id": "9719044729f6", "complexity": "O(n) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force oracle on 22 inputs; larg...
codehard-train-000019
[ { "role": "user", "content": "**Count inversions**\n\nReturn the number of index pairs `i < j` with `a[i] > a[j]`. Values can be negative and the list can have 150,000 elements, so checking every pair is too slow.\n\nSignature:\n```python\ndef inversion_count(a: list[int]) -> int:\n```\n\nExamples:\n```pyth...
{ "kind": "code.hard.inversions.plain", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "inversion_count", "problem_id": "ee7e58bf359d", "complexity": "O(n log n) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force oracle on 21 ...
codehard-train-000020
[ { "role": "user", "content": "**Fewest points that cover every interval**\n\nYou are given intervals `[start, end]` with `start < end`. Return the smallest number of points such that every interval contains at least one of them (endpoints included, so the point 2 covers both `[1, 2]` and `[2, 3]`). There ca...
{ "kind": "code.hard.interval_greedy.points", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "fewest_stabs", "problem_id": "cdf4939fe2d3", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "solve", "checked_by": "reference == brute-force...
codehard-train-000021
[ { "role": "user", "content": "**Task order**\n\nThere are `n` tasks `0..n-1` and pairs `[a, b]` meaning task `a` must be done before task `b`. Return the lexicographically smallest order that does every task once and respects all pairs, or an empty list if no valid order exists (a cycle).\n\nSignature:\n```...
{ "kind": "code.hard.topo_order.lexmin", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "schedule", "problem_id": "723aad79cf87", "complexity": "O((n + m) log n) time, O(n + m) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 1...
codehard-train-000022
[ { "role": "user", "content": "**Best time to buy and sell, one trade**\n\n`prices[i]` is the price of a share on day `i`. You may buy once and sell once later (or not trade at all). Return the maximum profit. There can be hundreds of thousands of days.\n\nSignature:\n```python\ndef max_profit_once(prices: l...
{ "kind": "code.hard.stock_profit.one", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "max_profit_once", "problem_id": "0405efd7f7e2", "complexity": "O(n) time, O(1) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 19 inputs; ...
codehard-train-000023
[ { "role": "user", "content": "**Count palindromic substrings**\n\nReturn how many non-empty substrings of `s` (counted by position) read the same backwards. `s` can be several thousand characters long, so checking every substring separately is too slow.\n\nSignature:\n```python\ndef count_palindromes(s: str...
{ "kind": "code.hard.palindromes.count", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "count_palindromes", "problem_id": "d8eb67dc967b", "complexity": "O(n²) time worst case, O(1) extra space", "mode": "explain", "checked_by": "reference == brute-forc...
codehard-train-000024
[ { "role": "user", "content": "**Longest increasing subsequence**\n\nReturn the length of the longest subsequence of `nums` (not necessarily contiguous) that is strictly increasing. `nums` can have up to 300,000 elements, so an O(n²) solution is too slow.\n\nSignature:\n```python\ndef lis_length(nums: list[i...
{ "kind": "code.hard.lis.strict", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "lis_length", "problem_id": "a9b07ae35241", "complexity": "O(n log n) time, O(n) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 19 inputs; large...
codehard-train-000025
[ { "role": "user", "content": "**Coin combinations**\n\nGiven distinct positive coin values (unlimited supply of each) and an amount, return the number of different combinations that add up to the amount, modulo 1,000,000,007. Order does not matter: 1+2 and 2+1 are the same combination.\n\nSignature:\n```pyt...
{ "kind": "code.hard.coin_change.ways", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "count_ways", "problem_id": "c64d9948a145", "complexity": "O(len(coins) · amount) time, O(amount) space", "mode": "explain", "checked_by": "reference == brute-force o...
codehard-train-000026
[ { "role": "user", "content": "**Coin combinations**\n\nGiven distinct positive coin values (unlimited supply of each) and an amount, return the number of different combinations that add up to the amount, modulo 1,000,000,007. Order does not matter: 1+2 and 2+1 are the same combination.\n\nSignature:\n```pyt...
{ "kind": "code.hard.coin_change.ways", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "coin_combinations", "problem_id": "c64d9948a145", "complexity": "O(len(coins) · amount) time, O(amount) space", "mode": "solve", "checked_by": "reference == brute-fo...
codehard-train-000027
[ { "role": "user", "content": "**Tasapainoiset sulkeet**\n\nPalauta True, jos jokainen `(`, `[` ja `{` merkkijonossa `s` suljetaan oikealla sulkeella oikeassa järjestyksessä, muuten False. Muut merkit ohitetaan.\n\nAllekirjoitus:\n```python\ndef valid_brackets(s: str) -> bool:\n```\n\nEsimerkit:\n```python\n...
{ "kind": "code.hard.brackets.valid", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "valid_brackets", "problem_id": "9719044729f6", "complexity": "O(n) time, O(n) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 22 inputs; lar...
codehard-train-000028
[ { "role": "user", "content": "**Top k words**\n\nReturn the `k` most frequent words in `words`, most frequent first. Words with the same count are ordered alphabetically. If there are fewer than `k` distinct words, return all of them.\n\nSignature:\n```python\ndef most_frequent(words: list[str], k: int) -> ...
{ "kind": "code.hard.top_k_words.alpha_ties", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "most_frequent", "problem_id": "c1541db84646", "complexity": "O(n + m log k) time, O(m) space", "mode": "explain", "checked_by": "reference == brute-force oracl...
codehard-train-000029
[ { "role": "user", "content": "**Evaluate an arithmetic expression**\n\nThe string contains non-negative integers, `+`, `-`, `*`, `/`, parentheses, spaces, and unary minus (as in `-3` or `-(2 + 1)`). Evaluate it with the usual precedence (`*` and `/` before `+` and `-`, left to right within a level). `/` is ...
{ "kind": "code.hard.expr_eval.shunting", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "evaluate", "problem_id": "ba1b3f285f18", "complexity": "O(n) time and space", "mode": "solve", "checked_by": "reference == brute-force oracle on 22 inputs; large i...
codehard-train-000030
[ { "role": "user", "content": "**Tehtävien järjestys**\n\nTehtäviä on `n` (`0..n-1`) ja pareja `[a, b]`, jotka tarkoittavat, että tehtävä `a` on tehtävä ennen tehtävää `b`. Palauta leksikografisesti pienin järjestys, jossa jokainen tehtävä tehdään kerran ja kaikkia pareja noudatetaan, tai tyhjä lista, jos ke...
{ "kind": "code.hard.topo_order.lexmin", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "smallest_topo_order", "problem_id": "723aad79cf87", "complexity": "O((n + m) log n) time, O(n + m) space", "mode": "solve", "checked_by": "reference == brute-force ...
codehard-train-000031
[ { "role": "user", "content": "**Sliding-window rate limiter**\n\nImplement `RateLimiter(limit, window)` with `allow(t)`. A call at time `t` is allowed if fewer than `limit` allowed calls happened in the window `(t - window, t]`; allowed calls are recorded, rejected ones are not. Times never decrease. Use am...
{ "kind": "code.hard.rate_limiter.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "RateLimiter", "problem_id": "60e4efc3c05f", "complexity": "amortised O(1) per call, O(limit) space", "mode": "solve", "checked_by": "reference == brute-force oracl...
codehard-train-000032
[ { "role": "user", "content": "**Sliding-window rate limiter**\n\nImplement `RateLimiter(limit, window)` with `allow(t)`. A call at time `t` is allowed if fewer than `limit` allowed calls happened in the window `(t - window, t]`; allowed calls are recorded, rejected ones are not. Times never decrease. Use am...
{ "kind": "code.hard.rate_limiter.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "RateLimiter", "problem_id": "60e4efc3c05f", "complexity": "amortised O(1) per call, O(limit) space", "mode": "solve", "checked_by": "reference == brute-force oracl...
codehard-train-000033
[ { "role": "user", "content": "**Longest common subsequence**\n\nReturn the length of the longest common subsequence of `a` and `b` (characters in the same order, not necessarily adjacent). Both strings can be over a thousand characters long.\n\nSignature:\n```python\ndef lcs_length(a: str, b: str) -> int:\n...
{ "kind": "code.hard.common_sequence.subseq", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "lcs_length", "problem_id": "30d0908fce21", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "solve", "checked_by": "reference == brute-force o...
codehard-train-000034
[ { "role": "user", "content": "**Cheapest route to a target**\n\nA directed graph has `n` nodes (0..n-1) and edges `[u, v, w]` from `u` to `v` with weight `w >= 1`. Return the length of the shortest path from `src` to `dst`, or -1 if `dst` cannot be reached. There can be 100,000 nodes and 300,000 edges.\n\nS...
{ "kind": "code.hard.dijkstra.target", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "fastest_route", "problem_id": "a629b677aea7", "complexity": "O((n + m) log m) time, O(n + m) space", "mode": "explain", "checked_by": "reference == brute-force oracle...
codehard-train-000035
[ { "role": "user", "content": "**Longest substring without repeats**\n\nReturn the length of the longest substring of `s` in which no character appears twice. `s` can be hundreds of thousands of characters long.\n\nSignature:\n```python\ndef longest_unique_run(s: str) -> int:\n```\n\nExamples:\n```python\n>>...
{ "kind": "code.hard.longest_window.unique", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_unique_run", "problem_id": "d083d59e642a", "complexity": "O(n) time, O(alphabet) space", "mode": "tests", "checked_by": "reference == brute-force oracle...
codehard-train-000036
[ { "role": "user", "content": "**Pisin yhteinen alijono**\n\nPalauta merkkijonojen `a` ja `b` pisimmän yhteisen alijonon pituus (merkit samassa järjestyksessä, ei välttämättä vierekkäin). Molemmat jonot voivat olla yli tuhat merkkiä pitkiä.\n\nAllekirjoitus:\n```python\ndef longest_common_subsequence(a: str,...
{ "kind": "code.hard.common_sequence.subseq", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_common_subsequence", "problem_id": "30d0908fce21", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "tests", "checked_by": "reference ...
codehard-train-000037
[ { "role": "user", "content": "**Painotettu muokkausetäisyys**\n\nMuuta merkkijono `a` merkkijonoksi `b`. Merkin lisääminen maksaa 3, poistaminen 3 ja merkin korvaaminen eri merkillä 2. Palauta pienin kokonaiskustannus.\n\nAllekirjoitus:\n```python\ndef transform_cost(a: str, b: str) -> int:\n```\n\nEsimerki...
{ "kind": "code.hard.edit_distance.i3d3s2", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "transform_cost", "problem_id": "105e8b6c1a99", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "explain", "checked_by": "reference == brute-for...
codehard-train-000038
[ { "role": "user", "content": "**Top k words**\n\nReturn the `k` most frequent words in `words`, most frequent first. Words with the same count are ordered alphabetically. If there are fewer than `k` distinct words, return all of them.\n\nSignature:\n```python\ndef top_words(words: list[str], k: int) -> list...
{ "kind": "code.hard.top_k_words.alpha_ties", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "top_words", "problem_id": "6ee163e1699e", "complexity": "O(n + m log k) time, O(m) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 1...
codehard-train-000039
[ { "role": "user", "content": "**Sliding-window rate limiter**\n\nImplement `RateLimiter(limit, window)` with `allow(t)`. A call at time `t` is allowed if fewer than `limit` allowed calls happened in the window `(t - window, t]`; allowed calls are recorded, rejected ones are not. Times never decrease. Use am...
{ "kind": "code.hard.rate_limiter.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "RateLimiter", "problem_id": "60e4efc3c05f", "complexity": "amortised O(1) per call, O(limit) space", "mode": "solve", "checked_by": "reference == brute-force oracl...
codehard-train-000040
[ { "role": "user", "content": "**Longest palindromic substring**\n\nReturn the length of the longest substring of `s` that reads the same backwards. `s` can be several thousand characters long, so checking every substring separately is too slow.\n\nSignature:\n```python\ndef longest_palindrome_len(s: str) ->...
{ "kind": "code.hard.palindromes.longest", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_palindrome_len", "problem_id": "9faa9dcf328a", "complexity": "O(n²) time worst case, O(1) extra space", "mode": "solve", "checked_by": "reference == brute...
codehard-train-000041
[ { "role": "user", "content": "**Longest common substring**\n\nReturn the length of the longest string that appears as a contiguous substring in both `a` and `b`. Both strings can be over a thousand characters long.\n\nSignature:\n```python\ndef common_substring_len(a: str, b: str) -> int:\n```\n\nExamples:\...
{ "kind": "code.hard.common_sequence.substring", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "common_substring_len", "problem_id": "92f6b9711b3e", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "tests", "checked_by": "reference == ...
codehard-train-000042
[ { "role": "user", "content": "**Longest increasing subsequence**\n\nReturn the length of the longest subsequence of `nums` (not necessarily contiguous) that is strictly increasing. `nums` can have up to 300,000 elements, so an O(n²) solution is too slow.\n\nSignature:\n```python\ndef longest_run_up(nums: li...
{ "kind": "code.hard.lis.strict", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "longest_run_up", "problem_id": "a9b07ae35241", "complexity": "O(n log n) time, O(n) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 19 inputs; l...
codehard-train-000043
[ { "role": "user", "content": "**Versioned key-value store**\n\nImplement `TimeMap()` with `set(key, value, timestamp)` and `get(key, timestamp)`. get returns the value set for `key` at the largest timestamp that is <= `timestamp`, or an empty string if there is none. For each key, set is called with strictl...
{ "kind": "code.hard.time_map.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "TimeMap", "problem_id": "a6e61ba9ba95", "complexity": "O(1) set, O(log n) get", "mode": "solve", "checked_by": "reference == brute-force oracle on 16 inputs; large inp...
codehard-train-000044
[ { "role": "user", "content": "**LRU cache**\n\nImplement `LRUCache(capacity)` with `get(key)` (the value, or -1 if absent) and `put(key, value)`. Both count as a use of the key. When a put would exceed the capacity, evict the least recently used key first. Both operations must be O(1) on average.\n\nSignatu...
{ "kind": "code.hard.lru_cache.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "LRUCache", "problem_id": "e9eeef7af2fc", "complexity": "O(1) average per operation, O(capacity) space", "mode": "tests", "checked_by": "reference == brute-force oracl...
codehard-train-000045
[ { "role": "user", "content": "**Count big inversions**\n\nReturn the number of index pairs `i < j` with `a[i] > 2 * a[j]`. Values can be negative and the list can have 150,000 elements, so checking every pair is too slow.\n\nSignature:\n```python\ndef count_big_inversions(a: list[int]) -> int:\n```\n\nExamp...
{ "kind": "code.hard.inversions.double", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "count_big_inversions", "problem_id": "ee3155a925ea", "complexity": "O(n log n) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force oracle ...
codehard-train-000046
[ { "role": "user", "content": "**Versioned key-value store**\n\nImplement `VersionedStore()` with `set(key, value, timestamp)` and `get(key, timestamp)`. get returns the value set for `key` at the largest timestamp that is <= `timestamp`, or an empty string if there is none. For each key, set is called with ...
{ "kind": "code.hard.time_map.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "VersionedStore", "problem_id": "a6e61ba9ba95", "complexity": "O(1) set, O(log n) get", "mode": "explain", "checked_by": "reference == brute-force oracle on 16 inputs; ...
codehard-train-000047
[ { "role": "user", "content": "**k yleisintä sanaa**\n\nPalauta listan `words` `k` yleisintä sanaa yleisimmästä alkaen. Saman verran esiintyvät sanat järjestetään aakkosjärjestykseen. Jos erilaisia sanoja on alle `k`, palauta kaikki.\n\nAllekirjoitus:\n```python\ndef top_k_words(words: list[str], k: int) -> ...
{ "kind": "code.hard.top_k_words.alpha_ties", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "top_k_words", "problem_id": "c1541db84646", "complexity": "O(n + m log k) time, O(m) space", "mode": "tests", "checked_by": "reference == brute-force oracle on...
codehard-train-000048
[ { "role": "user", "content": "**Largest connected group**\n\nThere are `n` nodes numbered 0..n-1 and a list of undirected edges `[u, v]`. Return the number of nodes in the largest connected group (0 if `n == 0`). `n` and the number of edges can both be 200,000.\n\nSignature:\n```python\ndef biggest_componen...
{ "kind": "code.hard.union_find.largest", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "biggest_component", "problem_id": "2b655db75f55", "complexity": "O((n + m) α(n)) time, O(n) space", "mode": "solve", "checked_by": "reference == brute-force oracle...
codehard-train-000049
[ { "role": "user", "content": "**Shortest distances from a source**\n\nA directed graph has `n` nodes (0..n-1) and edges `[u, v, w]` from `u` to `v` with weight `w >= 1`. Return a list with the length of the shortest path from `src` to every node, or -1 for nodes that cannot be reached. There can be 100,000 ...
{ "kind": "code.hard.dijkstra.all", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "shortest_distances", "problem_id": "22d6698072a4", "complexity": "O((n + m) log m) time, O(n + m) space", "mode": "tests", "checked_by": "reference == brute-force oracle...
codehard-train-000050
[ { "role": "user", "content": "**LRU cache**\n\nImplement `LRUCache(capacity)` with `get(key)` (the value, or -1 if absent) and `put(key, value)`. Both count as a use of the key. When a put would exceed the capacity, evict the least recently used key first. Both operations must be O(1) on average.\n\nSignatu...
{ "kind": "code.hard.lru_cache.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "LRUCache", "problem_id": "7397c40637e6", "complexity": "O(1) average per operation, O(capacity) space", "mode": "tests", "checked_by": "reference == brute-force oracl...
codehard-train-000051
[ { "role": "user", "content": "**Count subarrays**\n\nReturn how many non-empty contiguous subarrays of `nums` there are whose sum is exactly `k`. Values can be negative and the list can have hundreds of thousands of elements.\n\nSignature:\n```python\ndef count_sum_k(nums: list[int], k: int) -> int:\n```\n\...
{ "kind": "code.hard.subarray_count.sum_k", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "count_sum_k", "problem_id": "30cbcb348f5f", "complexity": "O(n) time, O(n) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 21 inputs; ...
codehard-train-000052
[ { "role": "user", "content": "**Vähiten pisteitä, jotka kattavat jokaisen välin**\n\nSaat välit `[start, end]`, joissa `start < end`. Palauta pienin määrä pisteitä niin, että jokainen väli sisältää ainakin yhden niistä (päätepisteet mukaan lukien, joten piste 2 kattaa sekä välin `[1, 2]` että `[2, 3]`). Väl...
{ "kind": "code.hard.interval_greedy.points", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_points_to_cover", "problem_id": "cdf4939fe2d3", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "explain", "checked_by": "reference == br...
codehard-train-000053
[ { "role": "user", "content": "**LRU cache**\n\nImplement `LRUCache(capacity)` with `get(key)` (the value, or -1 if absent) and `put(key, value)`. Both count as a use of the key. When a put would exceed the capacity, evict the least recently used key first. Both operations must be O(1) on average.\n\nSignatu...
{ "kind": "code.hard.lru_cache.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "LRUCache", "problem_id": "e9eeef7af2fc", "complexity": "O(1) average per operation, O(capacity) space", "mode": "explain", "checked_by": "reference == brute-force ora...
codehard-train-000054
[ { "role": "user", "content": "**Paths through a grid**\n\nThe grid is a list of strings of '.' (open) and '#' (blocked). Starting at the top-left cell and moving only right or down, how many paths reach the bottom-right cell without stepping on '#'? Return the count modulo 1,000,000,007 (0 if a corner is bl...
{ "kind": "code.hard.grid_paths.count", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "count_paths", "problem_id": "9cbaa94810d4", "complexity": "O(rows · cols) time, O(cols) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 18...
codehard-train-000055
[ { "role": "user", "content": "**Longest common subsequence**\n\nReturn the length of the longest common subsequence of `a` and `b` (characters in the same order, not necessarily adjacent). Both strings can be over a thousand characters long.\n\nSignature:\n```python\ndef longest_common_subsequence(a: str, b...
{ "kind": "code.hard.common_sequence.subseq", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_common_subsequence", "problem_id": "30d0908fce21", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "solve", "checked_by": "reference ...
codehard-train-000056
[ { "role": "user", "content": "**Laske palindromiosajonot**\n\nPalauta montako merkkijonon `s` epätyhjää osajonoa (paikan mukaan laskettuna) on sama takaperin luettuna. `s` voi olla tuhansia merkkejä pitkä, joten jokaisen osajonon tarkistaminen erikseen on liian hidasta.\n\nAllekirjoitus:\n```python\ndef pal...
{ "kind": "code.hard.palindromes.count", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "palindromic_substrings", "problem_id": "d8eb67dc967b", "complexity": "O(n²) time worst case, O(1) extra space", "mode": "explain", "checked_by": "reference == brute...
codehard-train-000057
[ { "role": "user", "content": "**Vähiten poistettavia välejä**\n\nSaat välit `[start, end]`, joissa `start < end`. Palauta pienin määrä välejä, jotka on poistettava, jotta loput eivät mene päällekkäin. Välit, jotka vain koskettavat (`[1, 2]` ja `[2, 3]`), eivät mene päällekkäin. Välejä voi olla 300 000.\n\nA...
{ "kind": "code.hard.interval_greedy.remove", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "fewest_to_cancel", "problem_id": "80778b127a4e", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "solve", "checked_by": "reference == brute-f...
codehard-train-000058
[ { "role": "user", "content": "**Fewest words**\n\nReturn the smallest number of words from `words` that concatenate to exactly `s` (each word can be used any number of times), or -1 if it is impossible. The empty string needs 0 words. `s` can be tens of thousands of characters long.\n\nSignature:\n```python...
{ "kind": "code.hard.word_break.fewest", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_word_split", "problem_id": "80e4a77aa7a1", "complexity": "O(n · L) set lookups (L = longest word length), O(n) space", "mode": "solve", "checked_by": "reference...
codehard-train-000059
[ { "role": "user", "content": "**Knapsack (unbounded)**\n\nItem i weighs `weights[i]` and is worth `values[i]` (all positive integers). Return the largest total value whose total weight is at most `capacity`; each item can be taken any number of times.\n\nSignature:\n```python\ndef max_value(weights: list[in...
{ "kind": "code.hard.knapsack.unbounded", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "max_value", "problem_id": "bbd681649271", "complexity": "O(n · capacity) time, O(capacity) space", "mode": "solve", "checked_by": "reference == brute-force oracle ...
codehard-train-000060
[ { "role": "user", "content": "**Spiral order**\n\nReturn all elements of the `rows × cols` matrix in spiral order, starting at the top-left corner and going clockwise (right along the top row first), ending at the centre.\n\nSignature:\n```python\ndef spiral_order(matrix: list[list[int]]) -> list[int]:\n```...
{ "kind": "code.hard.spiral.cw", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "spiral_order", "problem_id": "38f2502ba7af", "complexity": "O(rows · cols) time, O(1) extra space besides the output", "mode": "solve", "checked_by": "reference == brute-fo...
codehard-train-000061
[ { "role": "user", "content": "**Knapsack (0/1)**\n\nItem i weighs `weights[i]` and is worth `values[i]` (all positive integers). Return the largest total value whose total weight is at most `capacity`; each item can be taken at most once.\n\nSignature:\n```python\ndef knapsack(weights: list[int], values: li...
{ "kind": "code.hard.knapsack.01", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "knapsack", "problem_id": "ad345a84d149", "complexity": "O(n · capacity) time, O(capacity) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 18 in...
codehard-train-000062
[ { "role": "user", "content": "**Largest square in a histogram**\n\n`heights[i]` is the height of bar `i` in a histogram; every bar is 1 wide and bars stand side by side. Return the area of the largest axis-aligned square that fits inside the bars. There can be hundreds of thousands of bars.\n\nSignature:\n`...
{ "kind": "code.hard.histogram.square0", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "largest_square", "problem_id": "d87fffcb5cc7", "complexity": "O(n) time, O(n) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 21 inputs; ...
codehard-train-000063
[ { "role": "user", "content": "**Longest substring with at most k kinds of characters**\n\nReturn the length of the longest substring of `s` that contains at most `k` distinct characters (`k >= 0`). `s` can be hundreds of thousands of characters long.\n\nSignature:\n```python\ndef longest_with_k_kinds(s: str...
{ "kind": "code.hard.longest_window.kdistinct", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_with_k_kinds", "problem_id": "6c59d711db68", "complexity": "O(n) time, O(k) space", "mode": "explain", "checked_by": "reference == brute-force oracle...
codehard-train-000064
[ { "role": "user", "content": "**Slowest speed that finishes in time**\n\nThere are piles of items `piles`. At speed `k` you take up to `k` items from one pile per hour (a pile with fewer items still takes the whole hour). Return the smallest integer speed that finishes every pile within `hours` hours (len(p...
{ "kind": "code.hard.search_answer.speed", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "slowest_speed", "problem_id": "aa30e8551e0b", "complexity": "O(n log R) time (R = size of the answer range), O(1) space", "mode": "solve", "checked_by": "referenc...
codehard-train-000065
[ { "role": "user", "content": "**Cheapest route**\n\nThere are `n` cities `0..n-1` and one-way flights `[u, v, price]` with positive prices. Return the cheapest total price from `src` to `dst`, or -1 if `dst` cannot be reached. `k` is ignored (kept for a uniform signature).\n\nSignature:\n```python\ndef chea...
{ "kind": "code.hard.cheapest_path.any", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "cheapest_price", "problem_id": "79c7a7a69f63", "complexity": "O(E log E) time, O(n + E) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 1...
codehard-train-000066
[ { "role": "user", "content": "**Longest non-decreasing subsequence**\n\nReturn the length of the longest subsequence of `nums` (not necessarily contiguous) that is non-decreasing (equal neighbours allowed). `nums` can have up to 300,000 elements, so an O(n²) solution is too slow.\n\nSignature:\n```python\nd...
{ "kind": "code.hard.lis.nondecreasing", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "lis_length", "problem_id": "53e040dea565", "complexity": "O(n log n) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force oracle on 19 inpu...
codehard-train-000067
[ { "role": "user", "content": "**Top k words**\n\nReturn the `k` most frequent words in `words`, most frequent first. Words with the same count are ordered alphabetically. If there are fewer than `k` distinct words, return all of them.\n\nSignature:\n```python\ndef top_k_words(words: list[str], k: int) -> li...
{ "kind": "code.hard.top_k_words.alpha_ties", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "top_k_words", "problem_id": "c1541db84646", "complexity": "O(n + m log k) time, O(m) space", "mode": "explain", "checked_by": "reference == brute-force oracle ...
codehard-train-000068
[ { "role": "user", "content": "**Ensimmäinen sykliä sulkeva kaari**\n\nSolmuja on `n`, numeroitu 0..n-1, ja suuntaamattomat kaaret annetaan listana `[u, v]`. Kaaret lisätään yksi kerrallaan annetussa järjestyksessä. Palauta ensimmäinen kaari `[u, v]`, jonka päät ovat jo yhteydessä, kun se lisätään (silmukka ...
{ "kind": "code.hard.union_find.first_cycle", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "first_cycle_edge", "problem_id": "68a840458140", "complexity": "O((n + m) α(n)) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force o...
codehard-train-000069
[ { "role": "user", "content": "**Pisin kasvava alijono**\n\nPalauta listan `nums` pisimmän alijonon (ei välttämättä yhtenäinen) pituus, kun alijonon on oltava aidosti kasvava. Listassa voi olla jopa 300 000 alkiota, joten O(n²)-ratkaisu on liian hidas.\n\nAllekirjoitus:\n```python\ndef longest_increasing(num...
{ "kind": "code.hard.lis.strict", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "longest_increasing", "problem_id": "a9b07ae35241", "complexity": "O(n log n) time, O(n) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 19 input...
codehard-train-000070
[ { "role": "user", "content": "**Knapsack (0/1)**\n\nItem i weighs `weights[i]` and is worth `values[i]` (all positive integers). Return the largest total value whose total weight is at most `capacity`; each item can be taken at most once.\n\nSignature:\n```python\ndef knapsack(weights: list[int], values: li...
{ "kind": "code.hard.knapsack.01", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "knapsack", "problem_id": "ad345a84d149", "complexity": "O(n · capacity) time, O(capacity) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 18 in...
codehard-train-000071
[ { "role": "user", "content": "**Korjaa sulkeet**\n\n`s` sisältää vain merkkejä `(` ja `)`. Palauta pienin määrä sulkeita, jotka on lisättävä (mihin tahansa), jotta jono on tasapainoinen.\n\nAllekirjoitus:\n```python\ndef fix_parens(s: str) -> int:\n```\n\nEsimerkit:\n```python\n>>> fix_parens('')\n0\n>>> fi...
{ "kind": "code.hard.brackets.min_insert", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "fix_parens", "problem_id": "f26917dfccd0", "complexity": "O(n) time, O(n) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 21 inputs; la...
codehard-train-000072
[ { "role": "user", "content": "**LRU cache**\n\nImplement `RecentCache(capacity)` with `get(key)` (the value, or -1 if absent) and `put(key, value)`. Both count as a use of the key. When a put would exceed the capacity, evict the least recently used key first. Both operations must be O(1) on average.\n\nSign...
{ "kind": "code.hard.lru_cache.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "RecentCache", "problem_id": "7397c40637e6", "complexity": "O(1) average per operation, O(capacity) space", "mode": "tests", "checked_by": "reference == brute-force or...
codehard-train-000073
[ { "role": "user", "content": "**Range sums with updates**\n\nImplement `RangeSum(nums)` with `update(index, value)`, which sets `nums[index] = value`, and `query(left, right)`, which returns the sum of `nums[left..right]` (both ends included, `left <= right`). There can be 100,000 values and 150,000 operati...
{ "kind": "code.hard.range_sum.class1", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "RangeSum", "problem_id": "b62ae4719863", "complexity": "O(n) to build, O(log n) per operation, O(n) space", "mode": "explain", "checked_by": "reference == brute-forc...
codehard-train-000074
[ { "role": "user", "content": "**Laske aritmeettinen lauseke**\n\nMerkkijonossa on ei-negatiivisia kokonaislukuja, `+`, `-`, `*`, `/`, sulkeita, välilyöntejä ja unaarinen miinus (kuten `-3` tai `-(2 + 1)`). Laske se tavallisella laskujärjestyksellä (`*` ja `/` ennen `+` ja `-`, samalla tasolla vasemmalta oik...
{ "kind": "code.hard.expr_eval.shunting", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "evaluate", "problem_id": "ba1b3f285f18", "complexity": "O(n) time and space", "mode": "solve", "checked_by": "reference == brute-force oracle on 22 inputs; large i...
codehard-train-000075
[ { "role": "user", "content": "**Prefix counter**\n\nImplement `PrefixCounter()` with three methods: `insert(word)` adds a word (duplicates count again), `count(word)` returns how many times exactly `word` was inserted, and `starts_with(prefix)` returns how many inserted words start with `prefix` (every word...
{ "kind": "code.hard.prefix_counter.class1", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "PrefixCounter", "problem_id": "f8b0e94bd962", "complexity": "O(len(word)) per operation, O(total length²) space for the prefixes", "mode": "solve", "checked_by"...
codehard-train-000076
[ { "role": "user", "content": "**Sliding-window rate limiter**\n\nImplement `RateLimiter(limit, window)` with `allow(t)`. A call at time `t` is allowed if fewer than `limit` allowed calls happened in the window `(t - window, t]`; allowed calls are recorded, rejected ones are not. Times never decrease. Use am...
{ "kind": "code.hard.rate_limiter.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "RateLimiter", "problem_id": "60e4efc3c05f", "complexity": "amortised O(1) per call, O(limit) space", "mode": "tests", "checked_by": "reference == brute-force oracl...
codehard-train-000077
[ { "role": "user", "content": "**Minimum meeting rooms**\n\nEach meeting is `[start, end)`: it occupies a room from `start` up to, but not including, `end`. Return the minimum number of rooms needed so that no two meetings share a room at the same time. A meeting that ends at 10 frees its room for one that s...
{ "kind": "code.hard.meeting_rooms.rooms", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_meeting_rooms", "problem_id": "11d132d67532", "complexity": "O(n log n) time, O(n) space", "mode": "solve", "checked_by": "reference == brute-force oracle on ...
codehard-train-000078
[ { "role": "user", "content": "**Largest rectangle in a histogram**\n\n`heights[i]` is the height of bar `i` in a histogram; every bar is 1 wide and bars stand side by side. Return the area of the largest axis-aligned rectangle that fits inside the bars. There can be hundreds of thousands of bars.\n\nSignatu...
{ "kind": "code.hard.histogram.rect1", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "max_histogram_area", "problem_id": "88cfb8e3925e", "complexity": "O(n) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-force oracle on 21 inpu...
codehard-train-000079
[ { "role": "user", "content": "**Coin combinations**\n\nGiven distinct positive coin values (unlimited supply of each) and an amount, return the number of different combinations that add up to the amount, modulo 1,000,000,007. Order does not matter: 1+2 and 2+1 are the same combination.\n\nSignature:\n```pyt...
{ "kind": "code.hard.coin_change.ways", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "count_ways", "problem_id": "c64d9948a145", "complexity": "O(len(coins) · amount) time, O(amount) space", "mode": "solve", "checked_by": "reference == brute-force ora...
codehard-train-000080
[ { "role": "user", "content": "**Largest rectangle in a histogram**\n\n`heights[i]` is the height of bar `i` in a histogram; every bar is 1 wide and bars stand side by side. Return the area of the largest axis-aligned rectangle that fits inside the bars. There can be hundreds of thousands of bars.\n\nSignatu...
{ "kind": "code.hard.histogram.rect0", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "max_histogram_area", "problem_id": "b594cfa644b5", "complexity": "O(n) time, O(n) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 21 inputs...
codehard-train-000081
[ { "role": "user", "content": "**Shortest distances from a source**\n\nA directed graph has `n` nodes (0..n-1) and edges `[u, v, w]` from `u` to `v` with weight `w >= 1`. Return a list with the length of the shortest path from `src` to every node, or -1 for nodes that cannot be reached. There can be 100,000 ...
{ "kind": "code.hard.dijkstra.all", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "shortest_distances", "problem_id": "22d6698072a4", "complexity": "O((n + m) log m) time, O(n + m) space", "mode": "tests", "checked_by": "reference == brute-force oracle...
codehard-train-000082
[ { "role": "user", "content": "**Fewest intervals to remove**\n\nYou are given intervals `[start, end]` with `start < end`. Return the smallest number of intervals to remove so that the rest do not overlap. Intervals that only touch (`[1, 2]` and `[2, 3]`) do not overlap. There can be 300,000 intervals.\n\nS...
{ "kind": "code.hard.interval_greedy.remove", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_removals", "problem_id": "80778b127a4e", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "explain", "checked_by": "reference == brute-for...
codehard-train-000083
[ { "role": "user", "content": "**Fewest points that cover every interval**\n\nYou are given intervals `[start, end]` with `start < end`. Return the smallest number of points such that every interval contains at least one of them (endpoints included, so the point 2 covers both `[1, 2]` and `[2, 3]`). There ca...
{ "kind": "code.hard.interval_greedy.points", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_points_to_cover", "problem_id": "cdf4939fe2d3", "complexity": "O(n log n) time for the sort, O(n) space", "mode": "tests", "checked_by": "reference == brut...
codehard-train-000084
[ { "role": "user", "content": "**Top k words**\n\nReturn the `k` most frequent words in `words`, most frequent first. Words with the same count are ordered alphabetically. If there are fewer than `k` distinct words, return all of them.\n\nSignature:\n```python\ndef top_words(words: list[str], k: int) -> list...
{ "kind": "code.hard.top_k_words.alpha_ties", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "top_words", "problem_id": "6ee163e1699e", "complexity": "O(n + m log k) time, O(m) space", "mode": "solve", "checked_by": "reference == brute-force oracle on 1...
codehard-train-000085
[ { "role": "user", "content": "**Sliding-window rate limiter**\n\nImplement `SlidingWindowLimiter(limit, window)` with `allow(t)`. A call at time `t` is allowed if fewer than `limit` allowed calls happened in the window `(t - window, t]`; allowed calls are recorded, rejected ones are not. Times never decreas...
{ "kind": "code.hard.rate_limiter.class", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "SlidingWindowLimiter", "problem_id": "60e4efc3c05f", "complexity": "amortised O(1) per call, O(limit) space", "mode": "explain", "checked_by": "reference == brute-...
codehard-train-000086
[ { "role": "user", "content": "**Longest common subsequence**\n\nReturn the length of the longest common subsequence of `a` and `b` (characters in the same order, not necessarily adjacent). Both strings can be over a thousand characters long.\n\nSignature:\n```python\ndef lcs_length(a: str, b: str) -> int:\n...
{ "kind": "code.hard.common_sequence.subseq", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "lcs_length", "problem_id": "30d0908fce21", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "solve", "checked_by": "reference == brute-force o...
codehard-train-000087
[ { "role": "user", "content": "**Jaksopituuskoodaus**\n\n`s` sisältää vain pieniä kirjaimia. Korvaa jokainen samojen kirjainten jakso kirjaimella ja jakson pituudella (jokainen jakso kirjoitetaan kirjaimena ja pituutena, myös pituus 1). Esimerkki: 'aaab' muuttuu muotoon a3b1.\n\nAllekirjoitus:\n```python\nde...
{ "kind": "code.hard.rle.always", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "rle_encode", "problem_id": "082a4c4820df", "complexity": "O(n) time and space", "mode": "solve", "checked_by": "reference == brute-force oracle on 18 inputs; large input u...
codehard-train-000088
[ { "role": "user", "content": "**Longest substring with at most k kinds of characters**\n\nReturn the length of the longest substring of `s` that contains at most `k` distinct characters (`k >= 0`). `s` can be hundreds of thousands of characters long.\n\nSignature:\n```python\ndef longest_with_k_kinds(s: str...
{ "kind": "code.hard.longest_window.kdistinct", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_with_k_kinds", "problem_id": "6c59d711db68", "complexity": "O(n) time, O(k) space", "mode": "explain", "checked_by": "reference == brute-force oracle...
codehard-train-000089
[ { "role": "user", "content": "**Range sums with updates**\n\nImplement `RangeSum(nums)` with `update(index, value)`, which sets `nums[index] = value`, and `query(left, right)`, which returns the sum of `nums[left..right]` (both ends included, `left <= right`). There can be 100,000 values and 150,000 operati...
{ "kind": "code.hard.range_sum.class0", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "RangeSum", "problem_id": "26883a5d3c79", "complexity": "O(n) to build, O(log n) per operation, O(n) space", "mode": "explain", "checked_by": "reference == brute-forc...
codehard-train-000090
[ { "role": "user", "content": "**Word break**\n\nReturn True if `s` can be split into a sequence of one or more words from `words` (each word can be used any number of times), and False otherwise. The empty string counts as splittable. `s` can be tens of thousands of characters long.\n\nSignature:\n```python...
{ "kind": "code.hard.word_break.can", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "word_break", "problem_id": "b1a741a936bf", "complexity": "O(n · L) set lookups (L = longest word length), O(n) space", "mode": "tests", "checked_by": "reference == bru...
codehard-train-000091
[ { "role": "user", "content": "**First edge that closes a cycle**\n\nThere are `n` nodes numbered 0..n-1 and a list of undirected edges `[u, v]`. Edges are added one by one in the given order. Return the first edge `[u, v]` whose two ends are already connected when it is added (a self-loop counts), or `[]` i...
{ "kind": "code.hard.union_find.first_cycle", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "first_redundant_edge", "problem_id": "68a840458140", "complexity": "O((n + m) α(n)) time, O(n) space", "mode": "explain", "checked_by": "reference == brute-for...
codehard-train-000092
[ { "role": "user", "content": "**Longest substring without repeats**\n\nReturn the length of the longest substring of `s` in which no character appears twice. `s` can be hundreds of thousands of characters long.\n\nSignature:\n```python\ndef longest_unique_run(s: str) -> int:\n```\n\nExamples:\n```python\n>>...
{ "kind": "code.hard.longest_window.unique", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_unique_run", "problem_id": "d083d59e642a", "complexity": "O(n) time, O(alphabet) space", "mode": "solve", "checked_by": "reference == brute-force oracle...
codehard-train-000093
[ { "role": "user", "content": "**Välisummat päivityksillä**\n\nToteuta `RangeSum(nums)`, jossa `update(index, value)` asettaa `nums[index] = value` ja `query(left, right)` palauttaa summan `nums[left..right]` (molemmat päät mukaan lukien, `left <= right`). Arvoja voi olla 100 000 ja operaatioita 150 000, jot...
{ "kind": "code.hard.range_sum.class1", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "RangeSum", "problem_id": "b62ae4719863", "complexity": "O(n) to build, O(log n) per operation, O(n) space", "mode": "solve", "checked_by": "reference == brute-force ...
codehard-train-000094
[ { "role": "user", "content": "**Etuliitelaskuri**\n\nToteuta `WordIndex()`, jossa on kolme metodia: `insert(word)` lisää sanan (toistot lasketaan uudelleen), `count(word)` palauttaa, montako kertaa täsmälleen `word` on lisätty, ja `starts_with(prefix)` palauttaa, moniko lisätty sana alkaa etuliitteellä `pre...
{ "kind": "code.hard.prefix_counter.class0", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "WordIndex", "problem_id": "f4007ef9f63c", "complexity": "O(len(word)) per operation, O(total length) space", "mode": "solve", "checked_by": "reference == brute-...
codehard-train-000095
[ { "role": "user", "content": "**Stock trading with a cooldown**\n\n`prices[i]` is the price of a share on day `i`. You may complete as many trades as you like, holding at most one share at a time, but after a sale you cannot buy on the next day (one day of cooldown). Return the maximum profit. There can be ...
{ "kind": "code.hard.stock_profit.cooldown", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 3, "entry": "max_profit_cooldown", "problem_id": "15e492eafe74", "complexity": "O(n) time, O(1) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 19...
codehard-train-000096
[ { "role": "user", "content": "**Tasapainoiset sulkeet**\n\nPalauta True, jos jokainen `(`, `[` ja `{` merkkijonossa `s` suljetaan oikealla sulkeella oikeassa järjestyksessä, muuten False. Muut merkit ohitetaan.\n\nAllekirjoitus:\n```python\ndef is_balanced(s: str) -> bool:\n```\n\nEsimerkit:\n```python\n>>>...
{ "kind": "code.hard.brackets.valid", "lang": "fi", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 1, "entry": "is_balanced", "problem_id": "9719044729f6", "complexity": "O(n) time, O(n) space", "mode": "tests", "checked_by": "reference == brute-force oracle on 22 inputs; large ...
codehard-train-000097
[ { "role": "user", "content": "**Fewest words**\n\nReturn the smallest number of words from `words` that concatenate to exactly `s` (each word can be used any number of times), or -1 if it is impossible. The empty string needs 0 words. `s` can be tens of thousands of characters long.\n\nSignature:\n```python...
{ "kind": "code.hard.word_break.fewest", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "min_word_split", "problem_id": "80e4a77aa7a1", "complexity": "O(n · L) set lookups (L = longest word length), O(n) space", "mode": "explain", "checked_by": "referen...
codehard-train-000098
[ { "role": "user", "content": "**Longest substring without repeats**\n\nReturn the length of the longest substring of `s` in which no character appears twice. `s` can be hundreds of thousands of characters long.\n\nSignature:\n```python\ndef longest_distinct_substring(s: str) -> int:\n```\n\nExamples:\n```py...
{ "kind": "code.hard.longest_window.unique", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "longest_distinct_substring", "problem_id": "d083d59e642a", "complexity": "O(n) time, O(alphabet) space", "mode": "solve", "checked_by": "reference == brute-forc...
codehard-train-000099
[ { "role": "user", "content": "**Weighted edit distance**\n\nTurn string `a` into string `b`. Inserting a character costs 3, deleting one costs 2, and replacing one character with a different one costs 3. Return the minimum total cost.\n\nSignature:\n```python\ndef edit_cost(a: str, b: str) -> int:\n```\n\nE...
{ "kind": "code.hard.edit_distance.i3d2s3", "lang": "en", "generator": "kreatix-code-hard", "version": "0.1.0", "difficulty": 2, "entry": "edit_cost", "problem_id": "08af17e39ec5", "complexity": "O(len(a) · len(b)) time, O(len(b)) space", "mode": "explain", "checked_by": "reference == brute-force or...