leetctl 0.4.0

LeetCode command-line interface in Rust, with curated problem sets and filtered random picking.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3
"""Regenerates data/sets/*.toml from their upstream sources.

Run from the repo root:

    python3 scripts/gen_sets.py

Every set is keyed on the LeetCode frontend id (`fid`), which is what `Problem` in the local
cache is filtered on. Sources give slugs (and sometimes a malformed id), so slugs are the join
key and fids come from LeetCode's own problem index. A slug that does not resolve fails the run
rather than being guessed at or quietly dropped.

Regeneration is all-or-nothing: every set is fetched, resolved, and rendered before anything is
written, so a failure part-way through cannot leave data/sets/ half-updated.

Only the standard library is used, so this needs no virtualenv.
"""

import csv
import datetime
import io
import json
import os
import re
import sys
import urllib.request

USER_AGENT = "Mozilla/5.0 (compatible; leetctl-gen-sets/1.0)"

LEETCODE_PROBLEM_INDEX = "https://leetcode.com/api/problems/all/"
LEETCODE_GRAPHQL = "https://leetcode.com/graphql"
NEETCODE_SITE_DATA = (
    "https://raw.githubusercontent.com/neetcode-gh/leetcode/main/.problemSiteData.json"
)
# The MIT data file above covers only the 450 problems NeetCode publishes solutions for, and flags
# just blind75 / neetcode150. The NeetCode 250 membership exists only in the website's own bundle,
# so that is fetched too. Its script name is content-hashed, hence the page scrape for the name.
NEETCODE_PRACTICE_PAGE = "https://neetcode.io/practice/practice/neetcode250"
NEETCODE_BUNDLE_BASE = "https://neetcode.io/"
COMPANY_CSV_BASE = (
    "https://raw.githubusercontent.com/hxu296/"
    "leetcode-company-wise-problems-2022/main/companies"
)

REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUTPUT_DIR = os.path.join(REPO_ROOT, "data", "sets")

# `source_as_of` is the vintage of the DATA, not of the fetch — `generated_at` records the fetch.
# It is a plain date so nothing has to judge whether a set counts as "stale"; readers compare it
# against today themselves.
NEETCODE_SOURCE = {
    "source_url": "https://github.com/neetcode-gh/leetcode",
    "source_license": "MIT",
    "source_as_of": None,  # tracks upstream; filled in with the generation date
}
NEETCODE_BUNDLE_SOURCE = {
    "source_url": "https://neetcode.io/practice/practice/neetcode250",
    "source_license": "NeetCode list membership; problem identifiers only",
    "source_as_of": None,
}
LEETCODE_SOURCE = {
    "source_url": "https://leetcode.com/studyplan/",
    "source_license": "Proprietary (LeetCode); problem identifiers only",
    "source_as_of": None,
}
# Not live LeetCode company tags — those are Premium-gated. This is a community-collected
# snapshot of which problems carried each company's tag in 2022.
COMPANY_SOURCE = {
    "source_url": "https://github.com/hxu296/leetcode-company-wise-problems-2022",
    "source_license": "MIT",
    "source_as_of": "2022",
}

# The MIT data file flags only blind75 and neetcode150 across its 450 entries; neetcode250 comes
# from the website bundle instead (see neetcode_bundle_problems).
NEETCODE_SETS = [
    ("blind75", "Blind 75", "The original Blind 75 list, as tracked by NeetCode.", "blind75"),
    (
        "neetcode150",
        "NeetCode 150",
        "NeetCode's 150-problem core list, a superset of Blind 75.",
        "neetcode150",
    ),
    (
        "neetcode-all",
        "NeetCode All",
        "Every problem NeetCode publishes a solution for, grouped into 19 patterns.",
        None,
    ),
]

LEETCODE_STUDY_PLANS = [
    (
        "top-interview-150",
        "Top Interview 150",
        "LeetCode's official Top Interview 150 study plan.",
    ),
    (
        "top-100-liked",
        "Top 100 Liked",
        "LeetCode's official Top 100 Liked study plan.",
    ),
    (
        "leetcode-75",
        "LeetCode 75",
        "LeetCode's official 75-problem starter study plan.",
    ),
]

# LeetCode renamed these problems after the 2022 company data was collected. Each target was
# verified against the live problem index by frontend id, not guessed from the old name.
RENAMED_SLUGS = {
    "coin-change-2": "coin-change-ii",                                              # 518
    "increasing-subsequences": "non-decreasing-subsequences",                       # 491
    "add-and-search-word-data-structure-design":
        "design-add-and-search-words-data-structure",                               # 211
    "implement-strstr": "find-the-index-of-the-first-occurrence-in-a-string",        # 28
    "make-two-arrays-equal-by-reversing-sub-arrays":
        "make-two-arrays-equal-by-reversing-subarrays",                             # 1460
    "friend-circles": "number-of-provinces",                                        # 547
    "bulb-switcher-iii": "number-of-times-binary-string-is-prefix-aligned",          # 1375
}

COMPANIES = [
    ("google", "Google", "Google"),
    ("facebook", "Meta / Facebook", "Facebook"),
    ("amazon", "Amazon", "Amazon"),
    ("microsoft", "Microsoft", "Microsoft"),
]

# Generation fails if a set does not come out at exactly this size. For the curated lists the
# count is part of the name; for the company snapshots it is whatever the source held when the
# data was last reviewed. Either way a drift means a source changed and someone should look,
# rather than a set quietly shipping short.
EXPECTED_COUNTS = {
    "blind75": 75,
    "neetcode150": 150,
    "neetcode250": 250,
    "neetcode-all": 450,
    "top-interview-150": 150,
    "top-100-liked": 100,
    "leetcode-75": 75,
    "google": 488,
    "facebook": 371,
    "amazon": 592,
    "microsoft": 363,
}

# Slugs a source lists that deliberately have no LeetCode counterpart any more. Empty today:
# every slug across all ten sets resolves. An entry here must record why.
KNOWN_OMISSIONS = {}


def fetch(url, data=None):
    request = urllib.request.Request(url, data=data, headers={"User-Agent": USER_AGENT})
    if data is not None:
        request.add_header("Content-Type", "application/json")
    with urllib.request.urlopen(request, timeout=60) as response:
        return response.read()


def fetch_json(url, payload=None):
    body = json.dumps(payload).encode() if payload is not None else None
    return json.loads(fetch(url, body))


def slug_to_fid_index():
    """LeetCode's own problem index: title slug -> frontend id."""
    index = fetch_json(LEETCODE_PROBLEM_INDEX)
    return {
        pair["stat"]["question__title_slug"]: int(pair["stat"]["frontend_question_id"])
        for pair in index["stat_status_pairs"]
    }


def slug_from_problem_url(url):
    """https://leetcode.com/problems/two-sum/ -> two-sum"""
    return url.rstrip("/").rsplit("/", 1)[-1]


def neetcode_bundle_problems():
    """Every problem the NeetCode site knows about, with its list memberships.

    The site is an Angular app whose problem table is compiled into the main bundle rather than
    served as JSON, so this locates the content-hashed bundle from the practice page and reads the
    object literals out of it. Each looks like:

        {problem:"Two Sum",pattern:"Arrays & Hashing",link:"two-sum/",...,neetcode250:!0,...}

    `link` is the LeetCode slug, which is what we join on. (`ncLink` is NeetCode's own slug for the
    same problem — "two-integer-sum" for Two Sum — and is deliberately not used.)
    """
    page = fetch(NEETCODE_PRACTICE_PAGE).decode("utf-8", "replace")
    match = re.search(r'src="(main\.[0-9a-f]+\.js)"', page)
    if match is None:
        raise GenerationError(
            "could not find the main bundle on the NeetCode practice page; the site's markup "
            "changed and neetcode_bundle_problems() needs updating"
        )
    bundle = fetch(NEETCODE_BUNDLE_BASE + match.group(1)).decode("utf-8", "replace")

    problems = []
    for start in (m.start() for m in re.finditer(r'\{problem:"', bundle)):
        depth = 0
        for index in range(start, min(start + 4000, len(bundle))):
            if bundle[index] == "{":
                depth += 1
            elif bundle[index] == "}":
                depth -= 1
                if depth == 0:
                    problems.append(bundle[start:index + 1])
                    break

    def slug_of(literal):
        found = re.search(r'\blink:"((?:[^"\\]|\\.)*)"', literal)
        return found.group(1).strip("/") if found else None

    parsed = []
    for literal in problems:
        slug = slug_of(literal)
        if not slug:
            continue
        parsed.append({
            "slug": slug,
            "blind75": re.search(r"\bblind75:!0", literal) is not None,
            "neetcode150": re.search(r"\bneetcode150:!0", literal) is not None,
            "neetcode250": re.search(r"\bneetcode250:!0", literal) is not None,
        })

    if len(parsed) < 500:
        raise GenerationError(
            f"only {len(parsed)} problems parsed out of the NeetCode bundle, expected ~1000; "
            f"the bundle's shape changed"
        )
    return parsed


def check_neetcode_sources_agree(bundle, site_data):
    """The 250 list comes from the website bundle while the other NeetCode sets come from the MIT
    data file. Mixing two sources is only safe while they agree, so verify it rather than assume:
    the sets they both describe must be identical, and each list must nest inside the next."""
    for flag in ("blind75", "neetcode150"):
        from_bundle = {p["slug"] for p in bundle if p[flag]}
        from_file = {p["link"].strip("/") for p in site_data if p.get(flag)}
        if from_bundle != from_file:
            difference = ", ".join(sorted(from_bundle ^ from_file)[:10])
            raise GenerationError(
                f"NeetCode's website bundle and MIT data file disagree about `{flag}` "
                f"({len(from_bundle)} vs {len(from_file)} problems; differing: {difference}). "
                f"They have drifted apart — pick one source for all NeetCode sets."
            )

    blind = {p["slug"] for p in bundle if p["blind75"]}
    hundred_fifty = {p["slug"] for p in bundle if p["neetcode150"]}
    two_fifty = {p["slug"] for p in bundle if p["neetcode250"]}
    if not (blind <= hundred_fifty <= two_fifty):
        raise GenerationError(
            "NeetCode's lists no longer nest (blind75 in neetcode150 in neetcode250); "
            "the site changed how its lists relate"
        )


def neetcode_slugs(site_data, membership_flag):
    return [
        entry["link"].strip("/")
        for entry in site_data
        if membership_flag is None or entry.get(membership_flag)
    ]


def study_plan_slugs(plan_slug):
    query = (
        "query($slug: String!) { studyPlanV2Detail(planSlug: $slug) "
        "{ planSubGroups { questions { titleSlug } } } }"
    )
    payload = {"query": query, "variables": {"slug": plan_slug}}
    detail = fetch_json(LEETCODE_GRAPHQL, payload)["data"]["studyPlanV2Detail"]
    if detail is None:
        raise SystemExit(f"study plan {plan_slug!r} returned no detail")
    return [
        question["titleSlug"]
        for group in detail["planSubGroups"]
        for question in group["questions"]
    ]


def company_slugs(csv_name):
    text = fetch(f"{COMPANY_CSV_BASE}/{csv_name}.csv").decode()
    rows = csv.DictReader(io.StringIO(text))
    return [slug_from_problem_url(row["problem_link"]) for row in rows]


def toml_string(value):
    """TOML basic strings share JSON's escaping rules for the values we emit."""
    return json.dumps(value, ensure_ascii=False)


class GenerationError(Exception):
    """A set could not be generated correctly. Raised before anything is written."""


def resolve(slug, problem_slugs, fid_index):
    """Slug list -> [(fid, slug)]. Raises rather than silently dropping an entry."""
    resolved = []
    unresolved = []
    seen_fids = set()
    for problem_slug in problem_slugs:
        problem_slug = RENAMED_SLUGS.get(problem_slug, problem_slug)
        if problem_slug in KNOWN_OMISSIONS.get(slug, ()):
            continue
        fid = fid_index.get(problem_slug)
        if fid is None:
            unresolved.append(problem_slug)
            continue
        if fid in seen_fids:
            continue
        seen_fids.add(fid)
        resolved.append((fid, problem_slug))

    if unresolved:
        raise GenerationError(
            f"{slug}: {len(unresolved)} slug(s) do not resolve to a LeetCode frontend id: "
            f"{', '.join(unresolved)}.\n"
            f"  Either LeetCode renamed them — add the new slug to RENAMED_SLUGS, verified "
            f"against the live index by frontend id — or they were removed, in which case add "
            f"them to KNOWN_OMISSIONS[{slug!r}] with a reason."
        )

    expected = EXPECTED_COUNTS[slug]
    if len(resolved) != expected:
        raise GenerationError(
            f"{slug}: resolved {len(resolved)} problems, expected {expected}. "
            f"The source changed. Review the diff, then update EXPECTED_COUNTS."
        )

    return resolved


def render_set(slug, name, description, source, problem_slugs, fid_index, sort_by_fid):
    """Validate and render one set. Returns (slug, count, file contents) — writes nothing."""
    resolved = resolve(slug, problem_slugs, fid_index)
    if sort_by_fid:
        resolved.sort()

    generated_at = datetime.date.today().isoformat()
    lines = [
        "# @generated by scripts/gen_sets.py — do not edit by hand.",
        f"slug = {toml_string(slug)}",
        f"name = {toml_string(name)}",
        f"description = {toml_string(description)}",
        f"source_url = {toml_string(source['source_url'])}",
        f"source_license = {toml_string(source['source_license'])}",
        f"source_as_of = {toml_string(source['source_as_of'] or generated_at)}",
        f"generated_at = {toml_string(generated_at)}",
        "",
        "problems = [",
    ]
    lines += [f"  {{ fid = {fid}, slug = {toml_string(s)} }}," for fid, s in resolved]
    lines += ["]", ""]

    return slug, len(resolved), "\n".join(lines)


def write_all(rendered):
    """Replace every output file, each written atomically via a temporary file + rename."""
    for slug, count, contents in rendered:
        path = os.path.join(OUTPUT_DIR, f"{slug}.toml")
        temporary = f"{path}.tmp"
        with open(temporary, "w", encoding="utf-8") as handle:
            handle.write(contents)
        os.replace(temporary, path)
        print(f"{slug:<20} {count:>4} problems")


def main():
    os.makedirs(OUTPUT_DIR, exist_ok=True)

    print("fetching LeetCode problem index...")
    fid_index = slug_to_fid_index()
    print(f"  {len(fid_index)} problems indexed")

    print("fetching NeetCode site data...")
    site_data = fetch_json(NEETCODE_SITE_DATA)
    print(f"  {len(site_data)} problems")

    print("fetching NeetCode website bundle...")
    bundle = neetcode_bundle_problems()
    print(f"  {len(bundle)} problems")
    check_neetcode_sources_agree(bundle, site_data)

    # Everything is fetched, validated and rendered first; only then is anything written, so a
    # failure in the tenth set cannot leave the first nine rewritten against a half-read source.
    rendered = []
    try:
        for slug, name, description, flag in NEETCODE_SETS:
            rendered.append(render_set(
                slug, name, description, NEETCODE_SOURCE,
                neetcode_slugs(site_data, flag), fid_index, sort_by_fid=False,
            ))

        rendered.append(render_set(
            "neetcode250", "NeetCode 250",
            "NeetCode's 250-problem list, a superset of NeetCode 150.",
            NEETCODE_BUNDLE_SOURCE,
            [p["slug"] for p in bundle if p["neetcode250"]], fid_index, sort_by_fid=False,
        ))

        for slug, name, description in LEETCODE_STUDY_PLANS:
            rendered.append(render_set(
                slug, name, description, LEETCODE_SOURCE,
                study_plan_slugs(slug), fid_index, sort_by_fid=False,
            ))

        for slug, name, csv_name in COMPANIES:
            description = (
                f"Problems carrying the {name} tag in a community-collected 2022 snapshot."
            )
            rendered.append(render_set(
                slug, name, description, COMPANY_SOURCE,
                company_slugs(csv_name), fid_index, sort_by_fid=True,
            ))
    except GenerationError as error:
        print(f"\nerror: {error}\nNothing was written.", file=sys.stderr)
        raise SystemExit(1)

    write_all(rendered)


if __name__ == "__main__":
    main()