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,40 @@
|
|
|
1
|
+
# 104 · Longest Consecutive Sequence
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: arrays & hashing · Target: 25 min · NeetCode 150 · LeetCode 128
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get an unsorted integer array `nums`. Find the longest run of integers `x, x + 1, x + 2, …` whose values all appear somewhere in `nums` (in any positions), and return its length.
|
|
8
|
+
|
|
9
|
+
Duplicates count once. An empty array has a longest run of `0`.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `nums` | Output | Why |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `[100, 4, 200, 1, 3, 2]` | `4` | `1, 2, 3, 4` |
|
|
16
|
+
| `[0, 3, 7, 2, 5, 8, 4, 6, 0, 1]` | `9` | `0` through `8` |
|
|
17
|
+
| `[]` | `0` | nothing there |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `0 <= nums.length <= 10^5` (the performance test uses 30,000 numbers that form a single run)
|
|
22
|
+
- `-10^9 <= nums[i] <= 10^9`
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 104
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(n). Sorting first (O(n log n)) also passes the tests and is a good first answer; the interviewer will then ask for O(n).
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- What is the true worst-case cost of your solution? Walk through the input that makes it work hardest.
|
|
39
|
+
- Return the run itself, not just its length.
|
|
40
|
+
- What if the numbers arrive one at a time and you must report the longest run after each one?
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { longestConsecutive } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('104 longest consecutive sequence', () => {
|
|
6
|
+
it('finds a run hidden among other values', () => {
|
|
7
|
+
expect(longestConsecutive([100, 4, 200, 1, 3, 2])).toBe(4)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('ignores duplicates', () => {
|
|
11
|
+
expect(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])).toBe(9)
|
|
12
|
+
expect(longestConsecutive([1, 2, 2, 3])).toBe(3)
|
|
13
|
+
expect(longestConsecutive([5, 5, 5])).toBe(1)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('returns 0 for an empty array', () => {
|
|
17
|
+
expect(longestConsecutive([])).toBe(0)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('returns 1 for a single value or no neighbours', () => {
|
|
21
|
+
expect(longestConsecutive([42])).toBe(1)
|
|
22
|
+
expect(longestConsecutive([10, 30, 20])).toBe(1)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('handles negatives and runs that cross zero', () => {
|
|
26
|
+
expect(longestConsecutive([-1, -3, 0, -2, 1])).toBe(5)
|
|
27
|
+
expect(longestConsecutive([-5, -4, 10, 11, 12])).toBe(3)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('sorts numerically, not as strings', () => {
|
|
31
|
+
// Default .sort() puts 10 before 9, which breaks the run 8, 9, 10, 11.
|
|
32
|
+
expect(longestConsecutive([11, 9, 10, 8, 100])).toBe(4)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('picks the longer of two runs', () => {
|
|
36
|
+
expect(longestConsecutive([1, 2, 3, 10, 11, 12, 13, 14, 20])).toBe(5)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('handles values far apart', () => {
|
|
40
|
+
expect(longestConsecutive([-1_000_000_000, 1_000_000_000, 0])).toBe(1)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/** Deterministic Fisher–Yates shuffle, so the run is out of order. */
|
|
45
|
+
function shuffled(values: readonly number[]): number[] {
|
|
46
|
+
const out = [...values]
|
|
47
|
+
let seed = 12_345
|
|
48
|
+
for (let i = out.length - 1; i > 0; i--) {
|
|
49
|
+
seed = (seed * 1_103_515_245 + 12_345) % 2_147_483_648
|
|
50
|
+
const j = seed % (i + 1)
|
|
51
|
+
;[out[i], out[j]] = [out[j]!, out[i]!]
|
|
52
|
+
}
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
describe('104 performance', () => {
|
|
57
|
+
it('handles a shuffled run of 30,000 numbers in under 200 ms (walking up from every number is O(n²) here)', () => {
|
|
58
|
+
const nums = shuffled(Array.from({ length: 30_000 }, (_, i) => i - 15_000))
|
|
59
|
+
const ms = timeMs(() => expect(longestConsecutive(nums)).toBe(30_000))
|
|
60
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# 105 · Encode and Decode Strings
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: arrays & hashing · Target: 25 min · NeetCode 150 · LeetCode 271
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Design a way to send a list of strings over a channel that only carries **one** string.
|
|
8
|
+
|
|
9
|
+
- `encode(strs)` turns the list into a single string.
|
|
10
|
+
- `decode(s)` turns that string back into the original list.
|
|
11
|
+
|
|
12
|
+
The only requirement is that `decode(encode(strs))` gives back exactly `strs`: same length, same order, same contents. The strings can contain **any** characters, including whatever separator you might be tempted to use (`#`, `,`, digits, spaces, newlines, emoji). Empty strings and an empty list must both survive the round trip, and `[]`, `['']` and `['', '']` must all decode differently.
|
|
13
|
+
|
|
14
|
+
Don't lean on `JSON.stringify` / `JSON.parse` or other serialisers; the exercise is designing the format yourself. (The tests can't detect this, so it's on your honour.)
|
|
15
|
+
|
|
16
|
+
## Examples
|
|
17
|
+
|
|
18
|
+
| `strs` | `decode(encode(strs))` |
|
|
19
|
+
|---|---|
|
|
20
|
+
| `["neet", "code", "love", "you"]` | `["neet", "code", "love", "you"]` |
|
|
21
|
+
| `["we", "say", ":", "yes"]` | `["we", "say", ":", "yes"]` |
|
|
22
|
+
| `[]` | `[]` |
|
|
23
|
+
| `[""]` | `[""]` |
|
|
24
|
+
|
|
25
|
+
## Constraints
|
|
26
|
+
|
|
27
|
+
- `0 <= strs.length <= 200`
|
|
28
|
+
- `0 <= strs[i].length <= 200`
|
|
29
|
+
- `strs[i]` can contain any characters.
|
|
30
|
+
|
|
31
|
+
## Run it
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm watch 105
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
38
|
+
|
|
39
|
+
Time O(total length) for both `encode` and `decode` · Space O(total length) for the output.
|
|
40
|
+
|
|
41
|
+
</details>
|
|
42
|
+
|
|
43
|
+
## Follow-ups an interviewer might ask
|
|
44
|
+
|
|
45
|
+
- Why does joining with a plain separator fail? Give an input that breaks it.
|
|
46
|
+
- How would you make it work for a stream, where `decode` sees the data in chunks?
|
|
47
|
+
- How does your format handle emoji, and does that depend on how JavaScript measures string length?
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { decode, encode } from './solution'
|
|
3
|
+
|
|
4
|
+
const roundTrip = (strs: readonly string[]): string[] => decode(encode(strs))
|
|
5
|
+
|
|
6
|
+
describe('105 encode and decode strings', () => {
|
|
7
|
+
it('round-trips plain words', () => {
|
|
8
|
+
expect(roundTrip(['neet', 'code', 'love', 'you'])).toEqual(['neet', 'code', 'love', 'you'])
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('round-trips punctuation', () => {
|
|
12
|
+
expect(roundTrip(['we', 'say', ':', 'yes'])).toEqual(['we', 'say', ':', 'yes'])
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('round-trips an empty list', () => {
|
|
16
|
+
expect(roundTrip([])).toEqual([])
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('tells [], [""] and ["", ""] apart', () => {
|
|
20
|
+
expect(roundTrip([''])).toEqual([''])
|
|
21
|
+
expect(roundTrip(['', ''])).toEqual(['', ''])
|
|
22
|
+
expect(roundTrip(['', 'a', ''])).toEqual(['', 'a', ''])
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('round-trips strings that contain likely separators', () => {
|
|
26
|
+
const strs = ['#', '##', 'a#b', ',', 'x,y', ';', '|', ' ', '\n', '\t', '\\', '/', '"', "'", ':']
|
|
27
|
+
expect(roundTrip(strs)).toEqual(strs)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('round-trips strings that look like an encoding', () => {
|
|
31
|
+
const strs = ['5#hello', '12abc', '3#', '0#', '#3', '1', '10', '007', '4#abcd2#hi']
|
|
32
|
+
expect(roundTrip(strs)).toEqual(strs)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('round-trips unicode and emoji', () => {
|
|
36
|
+
const strs = ['héllo', '日本語', '😀', 'a😀b', '👩👩👧', 'Ω≈ç√']
|
|
37
|
+
expect(roundTrip(strs)).toEqual(strs)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('round-trips a long string next to short ones', () => {
|
|
41
|
+
const long = 'ab#'.repeat(100)
|
|
42
|
+
const strs = ['', long, 'z', long + '9']
|
|
43
|
+
expect(roundTrip(strs)).toEqual(strs)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('round-trips many strings without changing their order', () => {
|
|
47
|
+
const strs = Array.from({ length: 200 }, (_, i) => `${i}#${'x'.repeat(i % 7)}`)
|
|
48
|
+
expect(roundTrip(strs)).toEqual(strs)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# 106 · Two Sum II: Input Array Is Sorted
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: two pointers · Target: 25 min · NeetCode 150 · LeetCode 167
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`numbers` is sorted in non-decreasing order (it may contain repeats). Find the two elements at different positions that add up to `target`.
|
|
8
|
+
|
|
9
|
+
Return their positions as **1-based** indices `[index1, index2]` with `index1 < index2`. So if the answer is the first and third elements, return `[1, 3]`, not `[0, 2]`.
|
|
10
|
+
|
|
11
|
+
Every input has exactly one valid answer.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
| `numbers` | `target` | Output |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `[2, 7, 11, 15]` | `9` | `[1, 2]` |
|
|
18
|
+
| `[2, 3, 4]` | `6` | `[1, 3]` |
|
|
19
|
+
| `[-1, 0]` | `-1` | `[1, 2]` |
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `2 <= numbers.length <= 3 · 10^4` (the performance test uses 80,000)
|
|
24
|
+
- `-1000 <= numbers[i] <= 1000` in the examples; the performance test uses larger values.
|
|
25
|
+
- Sorted in non-decreasing order, with exactly one valid answer.
|
|
26
|
+
|
|
27
|
+
## Run it
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm watch 106
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
34
|
+
|
|
35
|
+
Time O(n) · Space **O(1)**. A hash map (as in 003) also runs in O(n) and will pass the tests, but it uses O(n) extra space and ignores the fact that the input is sorted. The tests can't measure memory, so this one is on your honour: get to O(1) space.
|
|
36
|
+
|
|
37
|
+
</details>
|
|
38
|
+
|
|
39
|
+
## Follow-ups an interviewer might ask
|
|
40
|
+
|
|
41
|
+
- Prove that your approach can never skip past the correct pair.
|
|
42
|
+
- Compare your approach with one that binary-searches for each element's partner.
|
|
43
|
+
- Return every distinct pair that adds up to `target`.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { twoSum } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('106 two sum II', () => {
|
|
6
|
+
it('finds the first two elements', () => {
|
|
7
|
+
expect(twoSum([2, 7, 11, 15], 9)).toEqual([1, 2])
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('finds elements that are not next to each other', () => {
|
|
11
|
+
expect(twoSum([2, 3, 4], 6)).toEqual([1, 3])
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('handles negatives and the minimum length', () => {
|
|
15
|
+
expect(twoSum([-1, 0], -1)).toEqual([1, 2])
|
|
16
|
+
expect(twoSum([-5, -3, 0, 2, 9], -8)).toEqual([1, 2])
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('returns 1-based indices with the smaller one first', () => {
|
|
20
|
+
expect(twoSum([1, 2, 3, 4, 4, 9, 56, 90], 146)).toEqual([7, 8])
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('uses two different positions with equal values', () => {
|
|
24
|
+
expect(twoSum([1, 3, 3, 8], 6)).toEqual([2, 3])
|
|
25
|
+
expect(twoSum([0, 0, 3, 4], 0)).toEqual([1, 2])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('does not reuse a single element', () => {
|
|
29
|
+
// 5 + 5 = 10, but there is only one 5. The real answer is 1 + 9.
|
|
30
|
+
expect(twoSum([1, 5, 9], 10)).toEqual([1, 3])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('handles repeated values around the answer', () => {
|
|
34
|
+
expect(twoSum([-3, -3, -3, 2, 7, 10, 10], 9)).toEqual([4, 5])
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
describe('106 performance', () => {
|
|
39
|
+
it('handles 80,000 numbers in under 200 ms (O(n²) is too slow here)', () => {
|
|
40
|
+
const n = 80_000
|
|
41
|
+
const numbers = Array.from({ length: n }, (_, i) => i * 3)
|
|
42
|
+
// Only the last two elements add up to the target.
|
|
43
|
+
const target = numbers[n - 1]! + numbers[n - 2]!
|
|
44
|
+
const ms = timeMs(() => expect(twoSum(numbers, target)).toEqual([n - 1, n]))
|
|
45
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# 107 · 3Sum
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: two pointers · Target: 30 min · NeetCode 150 · LeetCode 15
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get an integer array `nums`. Return every **distinct** triplet of values `[a, b, c]` that sums to `0`, where the three values come from three different positions in the array.
|
|
8
|
+
|
|
9
|
+
"Distinct" is about values, not positions: `[-1, 0, 1]` and `[0, 1, -1]` are the same triplet and must appear only once, even if the array has several `-1`s. Return the triplets in any order, and the values inside each triplet in any order.
|
|
10
|
+
|
|
11
|
+
Don't modify `nums` itself.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
| `nums` | Output (any order) |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `[-1, 0, 1, 2, -1, -4]` | `[[-1, -1, 2], [-1, 0, 1]]` |
|
|
18
|
+
| `[0, 1, 1]` | `[]` |
|
|
19
|
+
| `[0, 0, 0]` | `[[0, 0, 0]]` |
|
|
20
|
+
|
|
21
|
+
## Constraints
|
|
22
|
+
|
|
23
|
+
- `3 <= nums.length <= 3000` (the performance test uses 1,601)
|
|
24
|
+
- `-10^5 <= nums[i] <= 10^5`
|
|
25
|
+
|
|
26
|
+
## Run it
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pnpm watch 107
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
33
|
+
|
|
34
|
+
Time O(n²) · Space O(1) extra beyond the sorted copy and the output (sorting takes O(n log n), which O(n²) swamps). The hash-set-per-element approach is also O(n²) and passes, but its de-duplication is messier.
|
|
35
|
+
|
|
36
|
+
</details>
|
|
37
|
+
|
|
38
|
+
## Follow-ups an interviewer might ask
|
|
39
|
+
|
|
40
|
+
- Can you avoid duplicate triplets without storing the ones you've already found?
|
|
41
|
+
- 3Sum Closest (LeetCode 16): return the sum closest to a target.
|
|
42
|
+
- 4Sum (LeetCode 18): how does the pattern generalise to k-Sum?
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { threeSum } from './solution'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Sorts inside each triplet, then sorts the triplets, and turns -0 into 0.
|
|
7
|
+
* Keeps duplicates, so a repeated triplet still fails.
|
|
8
|
+
*/
|
|
9
|
+
function normalise(triplets: readonly (readonly number[])[]): number[][] {
|
|
10
|
+
return triplets
|
|
11
|
+
.map((t) => t.map((v) => (v === 0 ? 0 : v)).sort((a, b) => a - b))
|
|
12
|
+
.sort((a, b) => a[0]! - b[0]! || a[1]! - b[1]! || a[2]! - b[2]!)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('107 3sum', () => {
|
|
16
|
+
it('finds both triplets in the example', () => {
|
|
17
|
+
expect(normalise(threeSum([-1, 0, 1, 2, -1, -4]))).toEqual([
|
|
18
|
+
[-1, -1, 2],
|
|
19
|
+
[-1, 0, 1],
|
|
20
|
+
])
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('returns an empty list when nothing sums to zero', () => {
|
|
24
|
+
expect(threeSum([0, 1, 1])).toEqual([])
|
|
25
|
+
expect(threeSum([1, 2, -2, -1])).toEqual([])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('handles all zeros', () => {
|
|
29
|
+
expect(normalise(threeSum([0, 0, 0]))).toEqual([[0, 0, 0]])
|
|
30
|
+
expect(normalise(threeSum([0, 0, 0, 0, 0]))).toEqual([[0, 0, 0]])
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('does not repeat a triplet when values repeat', () => {
|
|
34
|
+
expect(normalise(threeSum([-2, 0, 0, 2, 2]))).toEqual([[-2, 0, 2]])
|
|
35
|
+
expect(normalise(threeSum([-1, -1, -1, 2, 2, 2]))).toEqual([[-1, -1, 2]])
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('finds triplets that reuse a value only when it appears enough times', () => {
|
|
39
|
+
// Only one 2, so [-4, 2, 2] is not allowed.
|
|
40
|
+
expect(normalise(threeSum([-4, 2, 1, 3]))).toEqual([[-4, 1, 3]])
|
|
41
|
+
expect(normalise(threeSum([-4, 2, 2]))).toEqual([[-4, 2, 2]])
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('finds many triplets', () => {
|
|
45
|
+
expect(normalise(threeSum([-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6]))).toEqual([
|
|
46
|
+
[-4, -2, 6],
|
|
47
|
+
[-4, 0, 4],
|
|
48
|
+
[-4, 1, 3],
|
|
49
|
+
[-4, 2, 2],
|
|
50
|
+
[-2, -2, 4],
|
|
51
|
+
[-2, 0, 2],
|
|
52
|
+
])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('does not modify the input', () => {
|
|
56
|
+
const nums = [3, -1, 0, -2, 1]
|
|
57
|
+
threeSum(nums)
|
|
58
|
+
expect(nums).toEqual([3, -1, 0, -2, 1])
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
describe('107 performance', () => {
|
|
63
|
+
it('handles 1,601 numbers in under 300 ms (O(n³) is too slow here)', () => {
|
|
64
|
+
// Odd numbers from -1599 to 1599 plus a single 0. Three odd numbers can't sum to 0,
|
|
65
|
+
// so the answers are exactly [-x, 0, x] for each odd x.
|
|
66
|
+
const nums = [0]
|
|
67
|
+
const expected: number[][] = []
|
|
68
|
+
for (let x = 1; x < 1_600; x += 2) {
|
|
69
|
+
nums.push(x, -x)
|
|
70
|
+
expected.push([-x, 0, x])
|
|
71
|
+
}
|
|
72
|
+
let result: number[][] = []
|
|
73
|
+
const ms = timeMs(() => {
|
|
74
|
+
result = threeSum(nums)
|
|
75
|
+
})
|
|
76
|
+
expect(normalise(result)).toEqual(normalise(expected))
|
|
77
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(300)
|
|
78
|
+
})
|
|
79
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 108 · Container With Most Water
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: two pointers · Target: 25 min · NeetCode 150 · LeetCode 11
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`height[i]` is the height of a vertical wall standing at position `i` on the x-axis. Pick two walls; together with the x-axis they form a container. The water it holds is
|
|
8
|
+
|
|
9
|
+
`(distance between the walls) × (height of the shorter wall)`.
|
|
10
|
+
|
|
11
|
+
Return the most water any pair of walls can hold. The walls in between don't get in the way.
|
|
12
|
+
|
|
13
|
+
## Examples
|
|
14
|
+
|
|
15
|
+
| `height` | Output | Why |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `[1, 8, 6, 2, 5, 4, 8, 3, 7]` | `49` | walls at positions 1 and 8: width 7 × height 7 |
|
|
18
|
+
| `[1, 1]` | `1` | width 1 × height 1 |
|
|
19
|
+
|
|
20
|
+
## Constraints
|
|
21
|
+
|
|
22
|
+
- `2 <= height.length <= 10^5` (the performance test uses 60,000)
|
|
23
|
+
- `0 <= height[i] <= 10^4`
|
|
24
|
+
|
|
25
|
+
## Run it
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm watch 108
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
32
|
+
|
|
33
|
+
Time O(n) · Space O(1).
|
|
34
|
+
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
## Follow-ups an interviewer might ask
|
|
38
|
+
|
|
39
|
+
- Prove that your approach never skips the best pair.
|
|
40
|
+
- Trapping Rain Water (LeetCode 42) looks similar. How is it different?
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { maxArea } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('108 container with most water', () => {
|
|
6
|
+
it('finds the best pair in the example', () => {
|
|
7
|
+
expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('handles two walls', () => {
|
|
11
|
+
expect(maxArea([1, 1])).toBe(1)
|
|
12
|
+
expect(maxArea([3, 9])).toBe(3)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('uses the shorter wall, not the taller one', () => {
|
|
16
|
+
expect(maxArea([1, 100])).toBe(1)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('handles zero-height walls', () => {
|
|
20
|
+
expect(maxArea([0, 0])).toBe(0)
|
|
21
|
+
expect(maxArea([0, 5, 0])).toBe(0)
|
|
22
|
+
expect(maxArea([0, 2, 0, 2, 0])).toBe(4)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('prefers a wide container over a tall narrow one', () => {
|
|
26
|
+
expect(maxArea([4, 3, 2, 1, 4])).toBe(16)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('prefers a tall narrow container over a wide shallow one', () => {
|
|
30
|
+
expect(maxArea([1, 2, 50, 50, 2, 1])).toBe(50)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('handles equal walls on both ends with taller ones inside', () => {
|
|
34
|
+
expect(maxArea([2, 9, 1, 1, 9, 2])).toBe(27)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('handles increasing and decreasing heights', () => {
|
|
38
|
+
expect(maxArea([1, 2, 3, 4, 5])).toBe(6)
|
|
39
|
+
expect(maxArea([5, 4, 3, 2, 1])).toBe(6)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe('108 performance', () => {
|
|
44
|
+
it('handles 60,000 walls in under 200 ms (O(n²) is too slow here)', () => {
|
|
45
|
+
const n = 60_000
|
|
46
|
+
const height = Array.from({ length: n }, (_, i) => i % 100)
|
|
47
|
+
// Two tall walls just inside the ends win: 10,000 × (n - 3).
|
|
48
|
+
height[1] = 10_000
|
|
49
|
+
height[n - 2] = 10_000
|
|
50
|
+
const ms = timeMs(() => expect(maxArea(height)).toBe(10_000 * (n - 3)))
|
|
51
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# 109 · Longest Substring Without Repeating Characters
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: sliding window · Target: 25 min · NeetCode 150 · LeetCode 3
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given a string `s`, return the length of the longest **substring** (a contiguous slice) in which no character appears twice.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| `s` | Output | Why |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `"abcabcbb"` | `3` | `"abc"` |
|
|
14
|
+
| `"bbbbb"` | `1` | `"b"` |
|
|
15
|
+
| `"pwwkew"` | `3` | `"wke"`; `"pwke"` doesn't count because it isn't contiguous |
|
|
16
|
+
| `""` | `0` | empty string |
|
|
17
|
+
|
|
18
|
+
## Constraints
|
|
19
|
+
|
|
20
|
+
- `0 <= s.length <= 5 · 10^4`
|
|
21
|
+
- `s` is **not** limited to ASCII. It can contain any characters, and the performance test uses a string of 20,000 characters drawn from 2,000 different Chinese characters.
|
|
22
|
+
- None of the test strings contain emoji, so indexing with `s[i]` is safe.
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 109
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(min(n, alphabet size)).
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- What changes if the input is guaranteed to be ASCII?
|
|
39
|
+
- Return the substring itself, not just its length.
|
|
40
|
+
- Longest substring with at most `k` distinct characters (LeetCode 340).
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { lengthOfLongestSubstring } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('109 longest substring without repeating characters', () => {
|
|
6
|
+
it('finds a repeating pattern', () => {
|
|
7
|
+
expect(lengthOfLongestSubstring('abcabcbb')).toBe(3)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('handles a single repeated character', () => {
|
|
11
|
+
expect(lengthOfLongestSubstring('bbbbb')).toBe(1)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('only counts contiguous characters', () => {
|
|
15
|
+
expect(lengthOfLongestSubstring('pwwkew')).toBe(3)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('handles empty and single-character strings', () => {
|
|
19
|
+
expect(lengthOfLongestSubstring('')).toBe(0)
|
|
20
|
+
expect(lengthOfLongestSubstring('a')).toBe(1)
|
|
21
|
+
expect(lengthOfLongestSubstring(' ')).toBe(1)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('handles a string with no repeats', () => {
|
|
25
|
+
expect(lengthOfLongestSubstring('abcdef')).toBe(6)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('does not move the window backwards on an old repeat', () => {
|
|
29
|
+
// At the second 'a', the last 'a' is outside the window already.
|
|
30
|
+
expect(lengthOfLongestSubstring('abba')).toBe(2)
|
|
31
|
+
expect(lengthOfLongestSubstring('tmmzuxt')).toBe(5)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('restarts just after the repeated character, not after the whole window', () => {
|
|
35
|
+
expect(lengthOfLongestSubstring('dvdf')).toBe(3)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('treats spaces, digits and symbols as characters', () => {
|
|
39
|
+
expect(lengthOfLongestSubstring('a b c')).toBe(3)
|
|
40
|
+
expect(lengthOfLongestSubstring('12!@1')).toBe(4)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('handles non-ASCII characters', () => {
|
|
44
|
+
expect(lengthOfLongestSubstring('日本日本語')).toBe(3)
|
|
45
|
+
expect(lengthOfLongestSubstring('éeée')).toBe(2)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
describe('109 performance', () => {
|
|
50
|
+
it('handles 20,000 characters from a 2,000-letter alphabet in under 200 ms (restarting from every index is O(n²) here)', () => {
|
|
51
|
+
const alphabet = Array.from({ length: 2_000 }, (_, i) => String.fromCharCode(0x4e00 + i)).join('')
|
|
52
|
+
const s = alphabet.repeat(10)
|
|
53
|
+
const ms = timeMs(() => expect(lengthOfLongestSubstring(s)).toBe(2_000))
|
|
54
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# 110 · Longest Repeating Character Replacement
|
|
2
|
+
|
|
3
|
+
**Level 2 · Medium** · Pattern: sliding window · Target: 30 min · NeetCode 150 · LeetCode 424
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get a string `s` of uppercase letters and an integer `k`. You may change any character of `s` into any other uppercase letter, at most `k` times in total.
|
|
8
|
+
|
|
9
|
+
Return the length of the longest substring you can make in which every character is the same letter.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `s` | `k` | Output | Why |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `"ABAB"` | `2` | `4` | change both `A`s to `B` (or both `B`s to `A`) |
|
|
16
|
+
| `"AABABBA"` | `1` | `4` | change the `B` in `"AABA"` to get `"AAAA"` |
|
|
17
|
+
| `"AAAA"` | `0` | `4` | already uniform |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `1 <= s.length <= 10^5` (the performance test uses 50,000)
|
|
22
|
+
- `s` contains only uppercase English letters `A`–`Z`.
|
|
23
|
+
- `0 <= k <= s.length`
|
|
24
|
+
|
|
25
|
+
## Run it
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm watch 110
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
32
|
+
|
|
33
|
+
Time O(n) · Space O(1) (26 counters). Recomputing the window's most common letter from the 26 counters at each step is O(26 · n), which is still linear and passes.
|
|
34
|
+
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
## Follow-ups an interviewer might ask
|
|
38
|
+
|
|
39
|
+
- What if `s` could contain any characters, not just `A`–`Z`?
|
|
40
|
+
- Max Consecutive Ones III (LeetCode 1004) is the binary version. How does your solution specialise to it?
|
|
41
|
+
- What if the replacements had different costs per letter?
|