interview-reps 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -2
- package/content/CLAUDE.md +131 -0
- package/content/LADDER.md +166 -0
- package/content/behavioral/QUESTIONS.md +93 -0
- package/content/behavioral/REVIEW.md +86 -0
- package/content/design/D01-short-links/README.md +20 -0
- package/content/design/D02-rate-limiter/README.md +21 -0
- package/content/design/D03-news-feed/README.md +20 -0
- package/content/design/D04-chat-messaging/README.md +20 -0
- package/content/design/D05-notification-service/README.md +20 -0
- package/content/design/D06-typeahead-search/README.md +20 -0
- package/content/design/D07-web-crawler/README.md +20 -0
- package/content/design/D08-file-sync/README.md +20 -0
- package/content/design/D09-ride-matching/README.md +20 -0
- package/content/design/D10-job-scheduler/README.md +20 -0
- package/content/design/D11-metrics-alerting/README.md +20 -0
- package/content/design/D12-payments-ledger/README.md +20 -0
- package/content/design/REVIEW.md +94 -0
- package/content/lib/binary-tree.ts +69 -0
- package/content/lib/graph.ts +50 -0
- package/content/lib/linked-list.ts +37 -0
- package/content/lib/perf.ts +9 -0
- package/content/problems/001-contains-duplicate/README.md +37 -0
- package/content/problems/001-contains-duplicate/solution.test.ts +38 -0
- package/content/problems/002-valid-anagram/README.md +36 -0
- package/content/problems/002-valid-anagram/solution.test.ts +38 -0
- package/content/problems/003-two-sum/README.md +40 -0
- package/content/problems/003-two-sum/solution.test.ts +40 -0
- package/content/problems/004-valid-palindrome/README.md +39 -0
- package/content/problems/004-valid-palindrome/solution.test.ts +38 -0
- package/content/problems/005-best-time-to-buy-and-sell-stock/README.md +38 -0
- package/content/problems/005-best-time-to-buy-and-sell-stock/solution.test.ts +39 -0
- package/content/problems/006-valid-parentheses/README.md +42 -0
- package/content/problems/006-valid-parentheses/solution.test.ts +41 -0
- package/content/problems/007-binary-search/README.md +39 -0
- package/content/problems/007-binary-search/solution.test.ts +56 -0
- package/content/problems/008-reverse-linked-list/README.md +39 -0
- package/content/problems/008-reverse-linked-list/solution.test.ts +41 -0
- package/content/problems/009-merge-two-sorted-lists/README.md +36 -0
- package/content/problems/009-merge-two-sorted-lists/solution.test.ts +43 -0
- package/content/problems/010-linked-list-cycle/README.md +37 -0
- package/content/problems/010-linked-list-cycle/solution.test.ts +39 -0
- package/content/problems/011-invert-binary-tree/README.md +39 -0
- package/content/problems/011-invert-binary-tree/solution.test.ts +32 -0
- package/content/problems/012-maximum-depth-of-binary-tree/README.md +37 -0
- package/content/problems/012-maximum-depth-of-binary-tree/solution.test.ts +29 -0
- package/content/problems/013-same-tree/README.md +37 -0
- package/content/problems/013-same-tree/solution.test.ts +38 -0
- package/content/problems/014-balanced-binary-tree/README.md +37 -0
- package/content/problems/014-balanced-binary-tree/solution.test.ts +38 -0
- package/content/problems/015-climbing-stairs/README.md +35 -0
- package/content/problems/015-climbing-stairs/solution.test.ts +27 -0
- package/content/problems/016-diameter-of-binary-tree/README.md +38 -0
- package/content/problems/016-diameter-of-binary-tree/solution.test.ts +63 -0
- package/content/problems/017-subtree-of-another-tree/README.md +42 -0
- package/content/problems/017-subtree-of-another-tree/solution.test.ts +61 -0
- package/content/problems/018-kth-largest-element-in-a-stream/README.md +49 -0
- package/content/problems/018-kth-largest-element-in-a-stream/solution.test.ts +104 -0
- package/content/problems/019-last-stone-weight/README.md +44 -0
- package/content/problems/019-last-stone-weight/solution.test.ts +67 -0
- package/content/problems/020-min-cost-climbing-stairs/README.md +40 -0
- package/content/problems/020-min-cost-climbing-stairs/solution.test.ts +44 -0
- package/content/problems/021-single-number/README.md +41 -0
- package/content/problems/021-single-number/solution.test.ts +46 -0
- package/content/problems/022-missing-number/README.md +40 -0
- package/content/problems/022-missing-number/solution.test.ts +44 -0
- package/content/problems/101-group-anagrams/README.md +41 -0
- package/content/problems/101-group-anagrams/solution.test.ts +83 -0
- package/content/problems/102-top-k-frequent-elements/README.md +44 -0
- package/content/problems/102-top-k-frequent-elements/solution.test.ts +59 -0
- package/content/problems/103-product-of-array-except-self/README.md +43 -0
- package/content/problems/103-product-of-array-except-self/solution.test.ts +59 -0
- package/content/problems/104-longest-consecutive-sequence/README.md +40 -0
- package/content/problems/104-longest-consecutive-sequence/solution.test.ts +62 -0
- package/content/problems/105-encode-and-decode-strings/README.md +47 -0
- package/content/problems/105-encode-and-decode-strings/solution.test.ts +50 -0
- package/content/problems/106-two-sum-ii-input-array-is-sorted/README.md +43 -0
- package/content/problems/106-two-sum-ii-input-array-is-sorted/solution.test.ts +47 -0
- package/content/problems/107-three-sum/README.md +42 -0
- package/content/problems/107-three-sum/solution.test.ts +79 -0
- package/content/problems/108-container-with-most-water/README.md +40 -0
- package/content/problems/108-container-with-most-water/solution.test.ts +53 -0
- package/content/problems/109-longest-substring-without-repeating-characters/README.md +40 -0
- package/content/problems/109-longest-substring-without-repeating-characters/solution.test.ts +56 -0
- package/content/problems/110-longest-repeating-character-replacement/README.md +41 -0
- package/content/problems/110-longest-repeating-character-replacement/solution.test.ts +51 -0
- package/content/problems/111-permutation-in-string/README.md +38 -0
- package/content/problems/111-permutation-in-string/solution.test.ts +54 -0
- package/content/problems/112-min-stack/README.md +51 -0
- package/content/problems/112-min-stack/solution.test.ts +123 -0
- package/content/problems/113-daily-temperatures/README.md +40 -0
- package/content/problems/113-daily-temperatures/solution.test.ts +56 -0
- package/content/problems/114-evaluate-reverse-polish-notation/README.md +44 -0
- package/content/problems/114-evaluate-reverse-polish-notation/solution.test.ts +56 -0
- package/content/problems/115-generate-parentheses/README.md +38 -0
- package/content/problems/115-generate-parentheses/solution.test.ts +59 -0
- package/content/problems/116-koko-eating-bananas/README.md +41 -0
- package/content/problems/116-koko-eating-bananas/solution.test.ts +53 -0
- package/content/problems/117-find-minimum-in-rotated-sorted-array/README.md +41 -0
- package/content/problems/117-find-minimum-in-rotated-sorted-array/solution.test.ts +69 -0
- package/content/problems/118-search-in-rotated-sorted-array/README.md +41 -0
- package/content/problems/118-search-in-rotated-sorted-array/solution.test.ts +95 -0
- package/content/problems/119-time-based-key-value-store/README.md +50 -0
- package/content/problems/119-time-based-key-value-store/solution.test.ts +107 -0
- package/content/problems/120-reorder-list/README.md +43 -0
- package/content/problems/120-reorder-list/solution.test.ts +83 -0
- package/content/problems/121-remove-nth-node-from-end-of-list/README.md +40 -0
- package/content/problems/121-remove-nth-node-from-end-of-list/solution.test.ts +55 -0
- package/content/problems/122-lru-cache/README.md +55 -0
- package/content/problems/122-lru-cache/solution.test.ts +152 -0
- package/content/problems/123-binary-tree-level-order-traversal/README.md +40 -0
- package/content/problems/123-binary-tree-level-order-traversal/solution.test.ts +42 -0
- package/content/problems/124-binary-tree-right-side-view/README.md +40 -0
- package/content/problems/124-binary-tree-right-side-view/solution.test.ts +56 -0
- package/content/problems/125-validate-binary-search-tree/README.md +46 -0
- package/content/problems/125-validate-binary-search-tree/solution.test.ts +78 -0
- package/content/problems/126-kth-smallest-element-in-a-bst/README.md +39 -0
- package/content/problems/126-kth-smallest-element-in-a-bst/solution.test.ts +61 -0
- package/content/problems/127-lowest-common-ancestor-of-a-bst/README.md +46 -0
- package/content/problems/127-lowest-common-ancestor-of-a-bst/solution.test.ts +106 -0
- package/content/problems/128-kth-largest-element-in-an-array/README.md +42 -0
- package/content/problems/128-kth-largest-element-in-an-array/solution.test.ts +67 -0
- package/content/problems/129-k-closest-points-to-origin/README.md +43 -0
- package/content/problems/129-k-closest-points-to-origin/solution.test.ts +74 -0
- package/content/problems/130-number-of-islands/README.md +44 -0
- package/content/problems/130-number-of-islands/solution.test.ts +69 -0
- package/content/problems/131-course-schedule/README.md +42 -0
- package/content/problems/131-course-schedule/solution.test.ts +82 -0
- package/content/problems/132-rotting-oranges/README.md +46 -0
- package/content/problems/132-rotting-oranges/solution.test.ts +74 -0
- package/content/problems/133-clone-graph/README.md +42 -0
- package/content/problems/133-clone-graph/solution.test.ts +58 -0
- package/content/problems/134-house-robber/README.md +38 -0
- package/content/problems/134-house-robber/solution.test.ts +45 -0
- package/content/problems/135-coin-change/README.md +40 -0
- package/content/problems/135-coin-change/solution.test.ts +45 -0
- package/content/problems/136-longest-increasing-subsequence/README.md +41 -0
- package/content/problems/136-longest-increasing-subsequence/solution.test.ts +53 -0
- package/content/problems/137-word-break/README.md +39 -0
- package/content/problems/137-word-break/solution.test.ts +49 -0
- package/content/problems/138-merge-intervals/README.md +40 -0
- package/content/problems/138-merge-intervals/solution.test.ts +84 -0
- package/content/problems/139-insert-interval/README.md +40 -0
- package/content/problems/139-insert-interval/solution.test.ts +75 -0
- package/content/problems/201-valid-sudoku/README.md +63 -0
- package/content/problems/201-valid-sudoku/solution.test.ts +104 -0
- package/content/problems/202-car-fleet/README.md +46 -0
- package/content/problems/202-car-fleet/solution.test.ts +85 -0
- package/content/problems/203-search-a-2d-matrix/README.md +47 -0
- package/content/problems/203-search-a-2d-matrix/solution.test.ts +108 -0
- package/content/problems/204-copy-list-with-random-pointer/README.md +46 -0
- package/content/problems/204-copy-list-with-random-pointer/solution.test.ts +120 -0
- package/content/problems/205-add-two-numbers/README.md +45 -0
- package/content/problems/205-add-two-numbers/solution.test.ts +74 -0
- package/content/problems/206-find-the-duplicate-number/README.md +47 -0
- package/content/problems/206-find-the-duplicate-number/solution.test.ts +72 -0
- package/content/problems/207-count-good-nodes/README.md +44 -0
- package/content/problems/207-count-good-nodes/solution.test.ts +63 -0
- package/content/problems/208-construct-tree-from-preorder-and-inorder/README.md +52 -0
- package/content/problems/208-construct-tree-from-preorder-and-inorder/solution.test.ts +126 -0
- package/content/problems/209-implement-trie/README.md +52 -0
- package/content/problems/209-implement-trie/solution.test.ts +138 -0
- package/content/problems/210-design-add-and-search-words/README.md +50 -0
- package/content/problems/210-design-add-and-search-words/solution.test.ts +127 -0
- package/content/problems/211-task-scheduler/README.md +47 -0
- package/content/problems/211-task-scheduler/solution.test.ts +64 -0
- package/content/problems/212-design-twitter/README.md +63 -0
- package/content/problems/212-design-twitter/solution.test.ts +137 -0
- package/content/problems/213-subsets/README.md +42 -0
- package/content/problems/213-subsets/solution.test.ts +63 -0
- package/content/problems/214-combination-sum/README.md +44 -0
- package/content/problems/214-combination-sum/solution.test.ts +75 -0
- package/content/problems/215-permutations/README.md +42 -0
- package/content/problems/215-permutations/solution.test.ts +70 -0
- package/content/problems/216-word-search/README.md +50 -0
- package/content/problems/216-word-search/solution.test.ts +86 -0
- package/content/problems/217-letter-combinations-of-a-phone-number/README.md +53 -0
- package/content/problems/217-letter-combinations-of-a-phone-number/solution.test.ts +57 -0
- package/content/problems/218-palindrome-partitioning/README.md +42 -0
- package/content/problems/218-palindrome-partitioning/solution.test.ts +77 -0
- package/content/problems/219-max-area-of-island/README.md +43 -0
- package/content/problems/219-max-area-of-island/solution.test.ts +78 -0
- package/content/problems/220-pacific-atlantic-water-flow/README.md +43 -0
- package/content/problems/220-pacific-atlantic-water-flow/solution.test.ts +86 -0
- package/content/problems/221-course-schedule-ii/README.md +42 -0
- package/content/problems/221-course-schedule-ii/solution.test.ts +107 -0
- package/content/problems/222-graph-valid-tree/README.md +42 -0
- package/content/problems/222-graph-valid-tree/solution.test.ts +75 -0
- package/content/problems/223-redundant-connection/README.md +42 -0
- package/content/problems/223-redundant-connection/solution.test.ts +68 -0
- package/content/problems/224-network-delay-time/README.md +44 -0
- package/content/problems/224-network-delay-time/solution.test.ts +70 -0
- package/content/problems/225-cheapest-flights-within-k-stops/README.md +45 -0
- package/content/problems/225-cheapest-flights-within-k-stops/solution.test.ts +85 -0
- package/content/problems/226-house-robber-ii/README.md +41 -0
- package/content/problems/226-house-robber-ii/solution.test.ts +51 -0
- package/content/problems/227-longest-palindromic-substring/README.md +41 -0
- package/content/problems/227-longest-palindromic-substring/solution.test.ts +69 -0
- package/content/problems/228-decode-ways/README.md +44 -0
- package/content/problems/228-decode-ways/solution.test.ts +50 -0
- package/content/problems/229-maximum-product-subarray/README.md +40 -0
- package/content/problems/229-maximum-product-subarray/solution.test.ts +55 -0
- package/content/problems/230-partition-equal-subset-sum/README.md +39 -0
- package/content/problems/230-partition-equal-subset-sum/solution.test.ts +54 -0
- package/content/problems/231-unique-paths/README.md +41 -0
- package/content/problems/231-unique-paths/solution.test.ts +45 -0
- package/content/problems/232-longest-common-subsequence/README.md +41 -0
- package/content/problems/232-longest-common-subsequence/solution.test.ts +48 -0
- package/content/problems/233-coin-change-ii/README.md +44 -0
- package/content/problems/233-coin-change-ii/solution.test.ts +45 -0
- package/content/problems/234-edit-distance/README.md +45 -0
- package/content/problems/234-edit-distance/solution.test.ts +48 -0
- package/content/problems/235-maximum-subarray/README.md +40 -0
- package/content/problems/235-maximum-subarray/solution.test.ts +63 -0
- package/content/problems/236-jump-game/README.md +40 -0
- package/content/problems/236-jump-game/solution.test.ts +68 -0
- package/content/problems/237-gas-station/README.md +42 -0
- package/content/problems/237-gas-station/solution.test.ts +56 -0
- package/content/problems/238-partition-labels/README.md +40 -0
- package/content/problems/238-partition-labels/solution.test.ts +54 -0
- package/content/problems/239-non-overlapping-intervals/README.md +40 -0
- package/content/problems/239-non-overlapping-intervals/solution.test.ts +61 -0
- package/content/problems/240-meeting-rooms-ii/README.md +42 -0
- package/content/problems/240-meeting-rooms-ii/solution.test.ts +65 -0
- package/content/problems/241-rotate-image/README.md +41 -0
- package/content/problems/241-rotate-image/solution.test.ts +72 -0
- package/content/problems/242-spiral-matrix/README.md +40 -0
- package/content/problems/242-spiral-matrix/solution.test.ts +90 -0
- package/content/problems/243-set-matrix-zeroes/README.md +44 -0
- package/content/problems/243-set-matrix-zeroes/solution.test.ts +153 -0
- package/content/problems/301-trapping-rain-water/README.md +42 -0
- package/content/problems/301-trapping-rain-water/solution.test.ts +63 -0
- package/content/problems/302-minimum-window-substring/README.md +43 -0
- package/content/problems/302-minimum-window-substring/solution.test.ts +56 -0
- package/content/problems/303-sliding-window-maximum/README.md +42 -0
- package/content/problems/303-sliding-window-maximum/solution.test.ts +63 -0
- package/content/problems/304-largest-rectangle-in-histogram/README.md +42 -0
- package/content/problems/304-largest-rectangle-in-histogram/solution.test.ts +57 -0
- package/content/problems/305-median-of-two-sorted-arrays/README.md +42 -0
- package/content/problems/305-median-of-two-sorted-arrays/solution.test.ts +84 -0
- package/content/problems/306-merge-k-sorted-lists/README.md +42 -0
- package/content/problems/306-merge-k-sorted-lists/solution.test.ts +72 -0
- package/content/problems/307-reverse-nodes-in-k-group/README.md +42 -0
- package/content/problems/307-reverse-nodes-in-k-group/solution.test.ts +86 -0
- package/content/problems/308-binary-tree-maximum-path-sum/README.md +44 -0
- package/content/problems/308-binary-tree-maximum-path-sum/solution.test.ts +91 -0
- package/content/problems/309-serialize-and-deserialize-binary-tree/README.md +50 -0
- package/content/problems/309-serialize-and-deserialize-binary-tree/solution.test.ts +120 -0
- package/content/problems/310-find-median-from-data-stream/README.md +47 -0
- package/content/problems/310-find-median-from-data-stream/solution.test.ts +102 -0
- package/content/problems/311-word-search-ii/README.md +51 -0
- package/content/problems/311-word-search-ii/solution.test.ts +144 -0
- package/content/problems/312-n-queens/README.md +41 -0
- package/content/problems/312-n-queens/solution.test.ts +91 -0
- package/content/problems/313-word-ladder/README.md +42 -0
- package/content/problems/313-word-ladder/solution.test.ts +85 -0
- package/content/problems/314-alien-dictionary/README.md +43 -0
- package/content/problems/314-alien-dictionary/solution.test.ts +127 -0
- package/content/problems/315-longest-increasing-path-in-a-matrix/README.md +42 -0
- package/content/problems/315-longest-increasing-path-in-a-matrix/solution.test.ts +118 -0
- package/content/problems/316-swim-in-rising-water/README.md +44 -0
- package/content/problems/316-swim-in-rising-water/solution.test.ts +138 -0
- package/content/product/P01-tool-lending/README.md +28 -0
- package/content/product/P02-city-bike-share/README.md +28 -0
- package/content/product/P03-caring-from-afar/README.md +29 -0
- package/content/product/P04-first-garden/README.md +28 -0
- package/content/product/P05-save-for-later/README.md +28 -0
- package/content/product/P06-ship-or-hold/README.md +28 -0
- package/content/product/P07-photo-backup-goals/README.md +28 -0
- package/content/product/P08-quarter-roadmap/README.md +28 -0
- package/content/product/P09-grocer-delivery/README.md +29 -0
- package/content/product/P10-notes-for-work/README.md +28 -0
- package/content/product/P11-fitness-coaching/README.md +28 -0
- package/content/product/P12-low-price-rival/README.md +28 -0
- package/content/product/P13-coffee-cups/README.md +28 -0
- package/content/product/P14-bike-repair-market/README.md +27 -0
- package/content/product/P15-scooter-rides/README.md +27 -0
- package/content/product/P16-school-lunch-boxes/README.md +28 -0
- package/content/product/P17-weekly-users-drop/README.md +28 -0
- package/content/product/P18-ride-cancellations/README.md +28 -0
- package/content/product/P19-signups-up-revenue-flat/README.md +28 -0
- package/content/product/P20-longer-sessions/README.md +28 -0
- package/content/product/REVIEW.md +107 -0
- package/dist/cli.mjs +4418 -0
- package/package.json +38 -4
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D06 · Typeahead search
|
|
2
|
+
|
|
3
|
+
Category: Search · Level: core · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Design the suggestions box under a search bar: as someone types, show the top few completions for what they have typed so far, based on what other people search for.
|
|
6
|
+
|
|
7
|
+
Ask how many searches happen per day, how many suggestion requests one search generates, how fresh the suggestions must be, how many suggestions to show, and whether results differ by language or region.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- About 5 billion searches a day; each one triggers a suggestion request on most keystrokes.
|
|
12
|
+
- Show the top 10 completions, ranked by how often they were searched.
|
|
13
|
+
- Suggestions should reflect new trends within an hour.
|
|
14
|
+
- Suggestions must come back in under 100 ms as the person types.
|
|
15
|
+
- Suggestions differ by language; personal history is out of scope.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- Offensive or legally blocked phrases must be removable within minutes. What changes?
|
|
20
|
+
- Suggestions should now include the person's own recent searches. What changes?
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D07 · Web crawler
|
|
2
|
+
|
|
3
|
+
Category: Data · Level: advanced · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Build a crawler that downloads pages from the public web so a search team can index them. It starts from a list of addresses, follows links it finds, and keeps the collection fresh over time.
|
|
6
|
+
|
|
7
|
+
Ask how many pages to collect, how often they should be refreshed, how polite the crawler must be to each site, what is stored for each page, and how much hardware is available.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- Target 5 billion pages, refreshed on average once a month; popular pages more often.
|
|
12
|
+
- Never send more than one request per second to any one site, and obey each site's crawl rules.
|
|
13
|
+
- Store the raw page and its fetch time; indexing is another team's job.
|
|
14
|
+
- Many pages are near-duplicates or traps that generate endless addresses.
|
|
15
|
+
- You have a few hundred machines.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- The search team wants news sites refreshed within 10 minutes of a change. What changes?
|
|
20
|
+
- Half the crawler machines are lost overnight. What happens to progress?
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D08 · File sync and sharing
|
|
2
|
+
|
|
3
|
+
Category: Storage · Level: advanced · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Design a service that keeps a person's files in sync across their laptop, phone and the web, and lets them share a file or folder with other people.
|
|
6
|
+
|
|
7
|
+
Ask how many users and how much data each stores, how large files can be, how quickly a change should reach other devices, what happens when two devices edit the same file offline, and what sharing permissions exist.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- 100 million users, storing 10 GB each on average.
|
|
12
|
+
- Files can be up to 50 GB; most are small documents and photos.
|
|
13
|
+
- A change should reach a person's other online devices within a few seconds.
|
|
14
|
+
- Devices can be offline for days and must catch up when they reconnect. If two devices change the same file, neither person's work may be lost.
|
|
15
|
+
- Sharing is view or edit, for single people or by link; edits by others sync the same way.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- Two people edit the same file while both offline, then reconnect. What do they see?
|
|
20
|
+
- Users ask to restore any file to how it was at any time in the last 30 days. What changes?
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D09 · Ride matching
|
|
2
|
+
|
|
3
|
+
Category: Marketplace · Level: advanced · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Design the part of a ride-hailing app that matches riders with nearby drivers: drivers report where they are, riders request a ride, and the system offers the ride to a suitable driver and tracks it to the end.
|
|
6
|
+
|
|
7
|
+
Ask how many drivers and riders are active at once, how often drivers report their location, how quickly a match must happen, what makes a driver suitable, and what happens when a driver declines.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- Up to 2 million drivers online at peak, worldwide, each sending a location every 4 seconds.
|
|
12
|
+
- About 20 million rides a day, concentrated in large cities.
|
|
13
|
+
- A rider should get a match, or a clear "no drivers", within 10 seconds.
|
|
14
|
+
- A suitable driver is online, free, and close by travel time, with the right car type. A driver gets at most one offer at a time and has 15 seconds to accept; if they decline, the ride goes to someone else.
|
|
15
|
+
- Out of scope: pricing, payments and route planning.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- A stadium empties and 50,000 people request rides from one area in 20 minutes. What happens?
|
|
20
|
+
- Riders can now share a car with others going the same way. What changes?
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D10 · Distributed job scheduler
|
|
2
|
+
|
|
3
|
+
Category: Infrastructure · Level: advanced · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Build a service that runs other teams' jobs at the times they ask for: once at a given time, or on a repeating schedule. Teams can see each run's status and logs, and failed runs are retried.
|
|
6
|
+
|
|
7
|
+
Ask how many jobs are registered, how many runs start per second at peak, how precise the start time must be, how long jobs run, and what guarantees teams need about a run happening exactly once.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- About 10 million registered jobs; most run hourly or daily.
|
|
12
|
+
- Many schedules start on the hour: about 200,000 runs start in the first second of each hour, against a few hundred per second the rest of the time.
|
|
13
|
+
- A run should start within a few seconds of its scheduled time.
|
|
14
|
+
- Jobs run from seconds to several hours on a shared pool of workers.
|
|
15
|
+
- A run must not be skipped; running it twice is bad but survivable if rare.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- A team needs job B to start only after job A succeeds. What changes?
|
|
20
|
+
- Part of the scheduler crashes at 11:59:58, just before the noon spike. What happens to the noon runs?
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D11 · Metrics and alerting
|
|
2
|
+
|
|
3
|
+
Category: Observability · Level: advanced · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Design a system that collects numeric measurements from every server and service in a company (request counts, latency, memory use), lets engineers chart them, and sends alerts when a rule is broken.
|
|
6
|
+
|
|
7
|
+
Ask how many sources send data and how often, how long data is kept and at what detail, how quickly an alert must fire, and how many charts and rules exist.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- About 1 million servers, each sending a few hundred measurements every 10 seconds.
|
|
12
|
+
- Full detail is kept for 2 weeks; coarser summaries for 1 year.
|
|
13
|
+
- An alert should fire within one minute of a rule being broken.
|
|
14
|
+
- About 100,000 alert rules, and dashboards that chart the last hour to the last year.
|
|
15
|
+
- Losing a few data points is acceptable; losing alerts is not.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- A bad deploy adds a new label to every measurement, so the number of distinct series grows 100x. What happens?
|
|
20
|
+
- The alerting system itself goes down. How would anyone find out?
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# D12 · Payments ledger
|
|
2
|
+
|
|
3
|
+
Category: Fintech · Level: advanced · Time: 45 min
|
|
4
|
+
|
|
5
|
+
Build the ledger behind a payments product: it records every movement of money between accounts (top-ups, payments, refunds, fees), keeps balances, and lets finance teams prove every balance from its history.
|
|
6
|
+
|
|
7
|
+
Ask how many transactions happen per second, what consistency is required for balances, how long records are kept, how outside payment providers report results, and what auditors need.
|
|
8
|
+
|
|
9
|
+
## If you ask
|
|
10
|
+
|
|
11
|
+
- About 10,000 transactions per second at peak, 200 million a day.
|
|
12
|
+
- A balance must never go negative, and money must never be created or lost.
|
|
13
|
+
- Records are kept for 7 years and can never be edited, only corrected by new entries.
|
|
14
|
+
- Outside providers confirm card payments later, sometimes hours later, and sometimes send the same result twice.
|
|
15
|
+
- Account holders see their balance and history in under a second.
|
|
16
|
+
|
|
17
|
+
## Follow-ups
|
|
18
|
+
|
|
19
|
+
- A provider resends a whole day of results after an outage. What happens?
|
|
20
|
+
- The company adds a second currency and conversions between them. What changes?
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# System design review protocol
|
|
2
|
+
|
|
3
|
+
The protocol the automatic reviewer follows after each design practice run (and anyone reviewing
|
|
4
|
+
one by hand: "review design D03"). The person practises without AI help. The review comments only
|
|
5
|
+
on what they wrote. **Never write the design, never name the component, technique or product that
|
|
6
|
+
would fix a gap, and never describe what a good answer contains.** For a weak area, name the KIND of
|
|
7
|
+
gap and the question they should ask themselves next time.
|
|
8
|
+
|
|
9
|
+
## Before writing
|
|
10
|
+
|
|
11
|
+
1. Read the prompt: `design/<Dnn-slug>/README.md`, including its `## If you ask` answers.
|
|
12
|
+
2. Read the person's notes and timing (the app passes them in the review request).
|
|
13
|
+
3. Read earlier reviews of the same prompt in `.reps/design/reviews/<Dnn>-*.md`, if any, and note
|
|
14
|
+
their gaps and "Next time" lines.
|
|
15
|
+
|
|
16
|
+
Do not run commands and do not write files: print the review only.
|
|
17
|
+
|
|
18
|
+
## Scores
|
|
19
|
+
|
|
20
|
+
Score each dimension 1 to 4, with a one-line reason that points at their own notes (quote a few
|
|
21
|
+
words where it helps).
|
|
22
|
+
|
|
23
|
+
| Score | Meaning |
|
|
24
|
+
|---|---|
|
|
25
|
+
| 1 | Missing, or so thin it could not guide a design. |
|
|
26
|
+
| 2 | Present, but vague, incomplete, or not connected to the rest of the notes. |
|
|
27
|
+
| 3 | Solid: specific, complete for the prompt, and consistent with the other sections. |
|
|
28
|
+
| 4 | Interview-ready: specific, justified with their own numbers or reasons, and explained clearly. |
|
|
29
|
+
|
|
30
|
+
The dimensions, judged on the notes as written:
|
|
31
|
+
|
|
32
|
+
- **requirements**: Are the functional needs, the non-functional needs and what is out of scope
|
|
33
|
+
written down? Did they use the clarifying answers in the prompt rather than assume?
|
|
34
|
+
- **estimates**: Are there numbers for load, storage and bandwidth, with the arithmetic shown? Are
|
|
35
|
+
the units and orders of magnitude plausible? Are the numbers used later in the design?
|
|
36
|
+
- **api**: Does every feature in their requirements have a call, with inputs and outputs?
|
|
37
|
+
- **data-model**: Are the stored entities, their keys and how they are read stated? Does the model
|
|
38
|
+
fit the reads and writes their API implies?
|
|
39
|
+
- **high-level**: Does the data flow cover every requirement, end to end, without hand-waving
|
|
40
|
+
("some service handles it")?
|
|
41
|
+
- **deep-dive**: Did they pick one hard part and go below the surface: what fails at their
|
|
42
|
+
estimated load, and what they would do about it?
|
|
43
|
+
- **trade-offs**: For their main choices, did they say what each one costs and what they gave up?
|
|
44
|
+
- **communication**: Are the notes ordered and readable by someone else? Did the run fit the
|
|
45
|
+
target time, and do the later sections show the time reached them (rather than being empty)?
|
|
46
|
+
Only the total time is recorded, so never guess how long each phase took.
|
|
47
|
+
|
|
48
|
+
## Verdict
|
|
49
|
+
|
|
50
|
+
- **solid** only if no dimension scores below 3 **and** the run finished within the target time.
|
|
51
|
+
- Otherwise **redo**.
|
|
52
|
+
|
|
53
|
+
## Gaps
|
|
54
|
+
|
|
55
|
+
Use only these tags, comma-separated, or `none`:
|
|
56
|
+
|
|
57
|
+
| Tag | Use when |
|
|
58
|
+
|---|---|
|
|
59
|
+
| `design:requirements` | requirements scored 1-2, or later sections contradict or ignore them |
|
|
60
|
+
| `design:estimates` | estimates scored 1-2, or the numbers are never used |
|
|
61
|
+
| `design:api` | api scored 1-2 |
|
|
62
|
+
| `design:data-model` | data-model scored 1-2 |
|
|
63
|
+
| `design:high-level` | high-level scored 1-2 |
|
|
64
|
+
| `design:scaling` | the design does not hold at the person's own estimated load, and the notes don't notice |
|
|
65
|
+
| `design:deep-dive` | deep-dive scored 1-2 |
|
|
66
|
+
| `design:trade-offs` | trade-offs scored 1-2 |
|
|
67
|
+
| `design:pacing` | over the target time, or the early sections are full and the later ones empty or thin |
|
|
68
|
+
| `communication` | communication scored 1-2 |
|
|
69
|
+
|
|
70
|
+
## The review
|
|
71
|
+
|
|
72
|
+
Print exactly these header lines first (they are parsed by the app, so keep the form):
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
# Design review <Dnn> · <prompt name>
|
|
76
|
+
Prompt: <Dnn>
|
|
77
|
+
Date: YYYY-MM-DD
|
|
78
|
+
Verdict: solid | redo
|
|
79
|
+
Scores: requirements <1-4>, estimates <1-4>, api <1-4>, data-model <1-4>, high-level <1-4>, deep-dive <1-4>, trade-offs <1-4>, communication <1-4>
|
|
80
|
+
Gaps: <tags, or none>
|
|
81
|
+
Next time: <one concrete habit for the next run>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Then, briefly:
|
|
85
|
+
|
|
86
|
+
1. **Scores, one line each**: the dimension, the score, the reason from their notes.
|
|
87
|
+
2. **Time**: total minutes against the target, and which sections are empty or thin.
|
|
88
|
+
3. **What went well**: one or two specific things worth keeping.
|
|
89
|
+
4. **Weakest areas**: for each dimension below 3, the kind of gap and the question to ask
|
|
90
|
+
yourself (for example "the estimates stop before storage: what does a year of this cost?").
|
|
91
|
+
Never the answer.
|
|
92
|
+
5. **Did last time's feedback stick?** Compare with the earlier reviews of this prompt: say plainly
|
|
93
|
+
whether their gaps came back or were fixed. Skip if this is the first run.
|
|
94
|
+
6. **Next time**: one habit, the same as the header line, with a sentence on how to practise it.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Binary tree node, same shape as LeetCode's. */
|
|
2
|
+
export class TreeNode {
|
|
3
|
+
val: number
|
|
4
|
+
left: TreeNode | null
|
|
5
|
+
right: TreeNode | null
|
|
6
|
+
|
|
7
|
+
constructor(val = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
|
|
8
|
+
this.val = val
|
|
9
|
+
this.left = left
|
|
10
|
+
this.right = right
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Builds a tree from LeetCode-style level order, where `null` marks a missing child.
|
|
16
|
+
* Example: [3, 9, 20, null, null, 15, 7]
|
|
17
|
+
*/
|
|
18
|
+
export function treeFrom(levelOrder: readonly (number | null)[]): TreeNode | null {
|
|
19
|
+
const first = levelOrder[0]
|
|
20
|
+
if (first === undefined || first === null) return null
|
|
21
|
+
const root = new TreeNode(first)
|
|
22
|
+
const queue: TreeNode[] = [root]
|
|
23
|
+
let i = 1
|
|
24
|
+
for (let head = 0; head < queue.length && i < levelOrder.length; head++) {
|
|
25
|
+
const node = queue[head]!
|
|
26
|
+
const left = levelOrder[i++]
|
|
27
|
+
if (left !== undefined && left !== null) {
|
|
28
|
+
node.left = new TreeNode(left)
|
|
29
|
+
queue.push(node.left)
|
|
30
|
+
}
|
|
31
|
+
const right = levelOrder[i++]
|
|
32
|
+
if (right !== undefined && right !== null) {
|
|
33
|
+
node.right = new TreeNode(right)
|
|
34
|
+
queue.push(node.right)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return root
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Converts a tree back to level order with trailing nulls trimmed. */
|
|
41
|
+
export function treeToArray(root: TreeNode | null): (number | null)[] {
|
|
42
|
+
const out: (number | null)[] = []
|
|
43
|
+
const queue: (TreeNode | null)[] = [root]
|
|
44
|
+
for (let head = 0; head < queue.length; head++) {
|
|
45
|
+
const node = queue[head]
|
|
46
|
+
if (node) {
|
|
47
|
+
out.push(node.val)
|
|
48
|
+
queue.push(node.left, node.right)
|
|
49
|
+
} else {
|
|
50
|
+
out.push(null)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
while (out.length > 0 && out.at(-1) === null) out.pop()
|
|
54
|
+
return out
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A degenerate tree (every node only has a right child): depth n, for stress tests. */
|
|
58
|
+
export function rightSpine(n: number): TreeNode | null {
|
|
59
|
+
let root: TreeNode | null = null
|
|
60
|
+
for (let v = n; v >= 1; v--) root = new TreeNode(v, null, root)
|
|
61
|
+
return root
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Finds the first node with the given value (pre-order), or null. */
|
|
65
|
+
export function findNode(root: TreeNode | null, val: number): TreeNode | null {
|
|
66
|
+
if (!root) return null
|
|
67
|
+
if (root.val === val) return root
|
|
68
|
+
return findNode(root.left, val) ?? findNode(root.right, val)
|
|
69
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Undirected graph node, same shape as LeetCode's Clone Graph node. */
|
|
2
|
+
export class GraphNode {
|
|
3
|
+
val: number
|
|
4
|
+
neighbors: GraphNode[]
|
|
5
|
+
|
|
6
|
+
constructor(val = 0, neighbors: GraphNode[] = []) {
|
|
7
|
+
this.val = val
|
|
8
|
+
this.neighbors = neighbors
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Builds a graph from a 1-indexed adjacency list: adjList[i] lists the
|
|
14
|
+
* neighbours of node i + 1. Returns node 1, or null for an empty list.
|
|
15
|
+
*/
|
|
16
|
+
export function graphFrom(adjList: readonly (readonly number[])[]): GraphNode | null {
|
|
17
|
+
const nodes = adjList.map((_, i) => new GraphNode(i + 1))
|
|
18
|
+
adjList.forEach((neighbors, i) => {
|
|
19
|
+
nodes[i]!.neighbors = neighbors.map((v) => {
|
|
20
|
+
const node = nodes[v - 1]
|
|
21
|
+
if (!node) throw new Error(`Neighbour ${v} is out of range`)
|
|
22
|
+
return node
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
return nodes[0] ?? null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Collects every node reachable from `start`, in BFS order. */
|
|
29
|
+
export function graphNodes(start: GraphNode | null): GraphNode[] {
|
|
30
|
+
if (!start) return []
|
|
31
|
+
const seen = new Set<GraphNode>([start])
|
|
32
|
+
const order: GraphNode[] = [start]
|
|
33
|
+
for (let head = 0; head < order.length; head++) {
|
|
34
|
+
for (const next of order[head]!.neighbors) {
|
|
35
|
+
if (!seen.has(next)) {
|
|
36
|
+
seen.add(next)
|
|
37
|
+
order.push(next)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return order
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Converts a graph back to a 1-indexed adjacency list (nodes must be labelled 1..n). */
|
|
45
|
+
export function graphToAdjList(start: GraphNode | null): number[][] {
|
|
46
|
+
const nodes = graphNodes(start)
|
|
47
|
+
const out: number[][] = Array.from({ length: nodes.length }, () => [])
|
|
48
|
+
for (const node of nodes) out[node.val - 1] = node.neighbors.map((n) => n.val)
|
|
49
|
+
return out
|
|
50
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Singly linked list node, same shape as LeetCode's. */
|
|
2
|
+
export class ListNode {
|
|
3
|
+
val: number
|
|
4
|
+
next: ListNode | null
|
|
5
|
+
|
|
6
|
+
constructor(val = 0, next: ListNode | null = null) {
|
|
7
|
+
this.val = val
|
|
8
|
+
this.next = next
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Builds a list from an array. If `cyclePos` is a valid index, the tail's
|
|
14
|
+
* `next` points back to the node at that index (used by cycle problems).
|
|
15
|
+
*/
|
|
16
|
+
export function listFrom(values: readonly number[], cyclePos = -1): ListNode | null {
|
|
17
|
+
const nodes = values.map((v) => new ListNode(v))
|
|
18
|
+
nodes.forEach((node, i) => {
|
|
19
|
+
node.next = nodes[i + 1] ?? null
|
|
20
|
+
})
|
|
21
|
+
const tail = nodes.at(-1)
|
|
22
|
+
const target = nodes[cyclePos]
|
|
23
|
+
if (tail && target) tail.next = target
|
|
24
|
+
return nodes[0] ?? null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Converts a list to an array. Throws on a cycle instead of looping forever. */
|
|
28
|
+
export function listToArray(head: ListNode | null, maxLength = 1_000_000): number[] {
|
|
29
|
+
const out: number[] = []
|
|
30
|
+
let node = head
|
|
31
|
+
while (node) {
|
|
32
|
+
if (out.length >= maxLength) throw new Error('List is longer than maxLength — is there a cycle?')
|
|
33
|
+
out.push(node.val)
|
|
34
|
+
node = node.next
|
|
35
|
+
}
|
|
36
|
+
return out
|
|
37
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs `fn` once and returns how long it took in milliseconds.
|
|
3
|
+
* Used by the "performance" test in each problem to catch brute-force solutions.
|
|
4
|
+
*/
|
|
5
|
+
export function timeMs(fn: () => unknown): number {
|
|
6
|
+
const start = performance.now()
|
|
7
|
+
fn()
|
|
8
|
+
return performance.now() - start
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# 001 · Contains Duplicate
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: arrays & hashing · Target: 10 min · NeetCode 150 · LeetCode 217
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get an array of integers. Return `true` if any value shows up at least twice, and `false` if every value is distinct.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| Input | Output | Why |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `[1, 2, 3, 1]` | `true` | `1` appears twice |
|
|
14
|
+
| `[1, 2, 3, 4]` | `false` | all distinct |
|
|
15
|
+
| `[1, 1, 1, 3, 3, 4, 3, 2, 4, 2]` | `true` | several repeats |
|
|
16
|
+
|
|
17
|
+
## Constraints
|
|
18
|
+
|
|
19
|
+
- `1 <= nums.length <= 10^5`
|
|
20
|
+
- `-10^9 <= nums[i] <= 10^9`
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 001
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
Time O(n) · Space O(n). The sort-first approach (O(n log n) time, O(1) extra space if you may sort in place) is a fair trade-off to mention.
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- What if you can't use extra memory?
|
|
37
|
+
- What if the array is too big to fit in memory?
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { containsDuplicate } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('001 contains duplicate', () => {
|
|
6
|
+
it('finds a repeated value', () => {
|
|
7
|
+
expect(containsDuplicate([1, 2, 3, 1])).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('returns false when every value is distinct', () => {
|
|
11
|
+
expect(containsDuplicate([1, 2, 3, 4])).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('handles many repeats', () => {
|
|
15
|
+
expect(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])).toBe(true)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('handles a single element', () => {
|
|
19
|
+
expect(containsDuplicate([7])).toBe(false)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('handles negatives and zero', () => {
|
|
23
|
+
expect(containsDuplicate([-1, 0, 1, -1])).toBe(true)
|
|
24
|
+
expect(containsDuplicate([-3, -2, -1, 0])).toBe(false)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('finds duplicates that are far apart', () => {
|
|
28
|
+
expect(containsDuplicate([5, 9, 2, 8, 3, 7, 5])).toBe(true)
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('001 performance', () => {
|
|
33
|
+
it('handles 100,000 distinct values in under 200 ms (O(n²) is too slow here)', () => {
|
|
34
|
+
const nums = Array.from({ length: 100_000 }, (_, i) => i * 2 - 50_000)
|
|
35
|
+
const ms = timeMs(() => expect(containsDuplicate(nums)).toBe(false))
|
|
36
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# 002 · Valid Anagram
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: arrays & hashing · Target: 10 min · NeetCode 150 · LeetCode 242
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given two strings `s` and `t`, return `true` if `t` uses exactly the same letters as `s`, with the same counts, just possibly in a different order.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| `s` | `t` | Output |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `"anagram"` | `"nagaram"` | `true` |
|
|
14
|
+
| `"rat"` | `"car"` | `false` |
|
|
15
|
+
| `"aacc"` | `"ccac"` | `false` (counts differ) |
|
|
16
|
+
|
|
17
|
+
## Constraints
|
|
18
|
+
|
|
19
|
+
- `1 <= s.length, t.length <= 5 * 10^4`
|
|
20
|
+
- Both strings contain only lowercase English letters.
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 002
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
Time O(n) · Space O(1), since there are only 26 possible letters. Sorting both strings is O(n log n) and acceptable as a first answer.
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- What changes if the input can contain any Unicode character?
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { isAnagram } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('002 valid anagram', () => {
|
|
6
|
+
it('accepts a rearrangement', () => {
|
|
7
|
+
expect(isAnagram('anagram', 'nagaram')).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('rejects different letters', () => {
|
|
11
|
+
expect(isAnagram('rat', 'car')).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('rejects same letters with different counts', () => {
|
|
15
|
+
expect(isAnagram('aacc', 'ccac')).toBe(false)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('rejects different lengths', () => {
|
|
19
|
+
expect(isAnagram('ab', 'abb')).toBe(false)
|
|
20
|
+
expect(isAnagram('abc', 'ab')).toBe(false)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('accepts identical strings and single letters', () => {
|
|
24
|
+
expect(isAnagram('listen', 'listen')).toBe(true)
|
|
25
|
+
expect(isAnagram('a', 'a')).toBe(true)
|
|
26
|
+
expect(isAnagram('a', 'b')).toBe(false)
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe('002 performance', () => {
|
|
31
|
+
it('handles 50,000-letter strings in under 200 ms', () => {
|
|
32
|
+
const letters = 'abcdefghijklmnopqrstuvwxyz'
|
|
33
|
+
const s = Array.from({ length: 50_000 }, (_, i) => letters[(i * 7) % 26]).join('')
|
|
34
|
+
const t = [...s].reverse().join('')
|
|
35
|
+
const ms = timeMs(() => expect(isAnagram(s, t)).toBe(true))
|
|
36
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 003 · Two Sum
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: arrays & hashing · Target: 15 min · NeetCode 150 · LeetCode 1
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given an array of integers `nums` and an integer `target`, return the indices of the two different elements that add up to `target`.
|
|
8
|
+
|
|
9
|
+
Every input has exactly one valid answer, and you can't use the same index twice. Return the two indices in any order.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `nums` | `target` | Output |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `[2, 7, 11, 15]` | `9` | `[0, 1]` |
|
|
16
|
+
| `[3, 2, 4]` | `6` | `[1, 2]` |
|
|
17
|
+
| `[3, 3]` | `6` | `[0, 1]` |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `2 <= nums.length <= 10^4` (the performance test uses 60,000)
|
|
22
|
+
- `-10^9 <= nums[i], target <= 10^9`
|
|
23
|
+
- Exactly one valid answer exists.
|
|
24
|
+
|
|
25
|
+
## Run it
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm watch 003
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
32
|
+
|
|
33
|
+
Time O(n) · Space O(n), in one pass.
|
|
34
|
+
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
## Follow-ups an interviewer might ask
|
|
38
|
+
|
|
39
|
+
- What if the array were sorted? (Can you get O(1) extra space?)
|
|
40
|
+
- What if there could be many answers and you had to return all of them?
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { twoSum } from './solution'
|
|
4
|
+
|
|
5
|
+
const sorted = (pair: readonly number[]): number[] => [...pair].sort((a, b) => a - b)
|
|
6
|
+
|
|
7
|
+
describe('003 two sum', () => {
|
|
8
|
+
it('finds the first pair', () => {
|
|
9
|
+
expect(sorted(twoSum([2, 7, 11, 15], 9))).toEqual([0, 1])
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('finds a pair that is not at the start', () => {
|
|
13
|
+
expect(sorted(twoSum([3, 2, 4], 6))).toEqual([1, 2])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('uses two different indices with equal values', () => {
|
|
17
|
+
expect(sorted(twoSum([3, 3], 6))).toEqual([0, 1])
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('does not reuse the same index', () => {
|
|
21
|
+
// 3 + 3 = 6, but there is only one 3. The real answer is 2 + 4.
|
|
22
|
+
expect(sorted(twoSum([3, 2, 4, 9], 6))).toEqual([1, 2])
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('handles negatives and zero', () => {
|
|
26
|
+
expect(sorted(twoSum([-3, 4, 3, 90], 0))).toEqual([0, 2])
|
|
27
|
+
expect(sorted(twoSum([0, 4, 3, 0], 0))).toEqual([0, 3])
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
describe('003 performance', () => {
|
|
32
|
+
it('handles 60,000 numbers in under 200 ms (O(n²) is too slow here)', () => {
|
|
33
|
+
const n = 60_000
|
|
34
|
+
const nums = Array.from({ length: n }, (_, i) => i * 3)
|
|
35
|
+
// Only the last two elements add up to the target.
|
|
36
|
+
const target = nums[n - 1]! + nums[n - 2]!
|
|
37
|
+
const ms = timeMs(() => expect(sorted(twoSum(nums, target))).toEqual([n - 2, n - 1]))
|
|
38
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
39
|
+
})
|
|
40
|
+
})
|