interview-reps 0.0.0-stage → 0.4.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 +4462 -0
- package/package.json +38 -4
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 004 · Valid Palindrome
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: two pointers · Target: 15 min · NeetCode 150 · LeetCode 125
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
A phrase counts as a palindrome if, after lowercasing it and throwing away everything that isn't a letter or a digit, it reads the same forwards and backwards.
|
|
8
|
+
|
|
9
|
+
Given a string `s`, return `true` if it's a palindrome under that rule.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `s` | Output | Why |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `"A man, a plan, a canal: Panama"` | `true` | becomes `amanaplanacanalpanama` |
|
|
16
|
+
| `"race a car"` | `false` | becomes `raceacar` |
|
|
17
|
+
| `" "` | `true` | becomes the empty string |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `1 <= s.length <= 2 * 10^5`
|
|
22
|
+
- `s` contains printable ASCII characters.
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 004
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(1): two pointers moving inward, skipping characters in place. Building a cleaned copy is O(n) space; mention that it works and then improve it.
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- Can you do it without allocating a new string?
|
|
39
|
+
- What if you may delete at most one character? (LeetCode 680)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { isPalindrome } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('004 valid palindrome', () => {
|
|
6
|
+
it('ignores case, spaces and punctuation', () => {
|
|
7
|
+
expect(isPalindrome('A man, a plan, a canal: Panama')).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('rejects a non-palindrome', () => {
|
|
11
|
+
expect(isPalindrome('race a car')).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('treats a string with no letters or digits as a palindrome', () => {
|
|
15
|
+
expect(isPalindrome(' ')).toBe(true)
|
|
16
|
+
expect(isPalindrome('.,!?')).toBe(true)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('keeps digits', () => {
|
|
20
|
+
expect(isPalindrome('0P')).toBe(false)
|
|
21
|
+
expect(isPalindrome('1a2 2A1')).toBe(true)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('handles one character and two characters', () => {
|
|
25
|
+
expect(isPalindrome('a')).toBe(true)
|
|
26
|
+
expect(isPalindrome('ab')).toBe(false)
|
|
27
|
+
expect(isPalindrome('aA')).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
describe('004 performance', () => {
|
|
32
|
+
it('handles 200,000 characters in under 200 ms', () => {
|
|
33
|
+
const half = 'Ab1, '.repeat(20_000)
|
|
34
|
+
const s = half + [...half].reverse().join('')
|
|
35
|
+
const ms = timeMs(() => expect(isPalindrome(s)).toBe(true))
|
|
36
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# 005 · Best Time to Buy and Sell Stock
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: sliding window · Target: 15 min · NeetCode 150 · LeetCode 121
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`prices[i]` is a stock's price on day `i`. You may buy once and then sell once on a **later** day.
|
|
8
|
+
|
|
9
|
+
Return the largest profit you can make. If no trade makes money, return `0`.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `prices` | Output | Why |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `[7, 1, 5, 3, 6, 4]` | `5` | buy at 1 (day 1), sell at 6 (day 4) |
|
|
16
|
+
| `[7, 6, 4, 3, 1]` | `0` | prices only fall |
|
|
17
|
+
|
|
18
|
+
## Constraints
|
|
19
|
+
|
|
20
|
+
- `1 <= prices.length <= 10^5`
|
|
21
|
+
- `0 <= prices[i] <= 10^4`
|
|
22
|
+
|
|
23
|
+
## Run it
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm watch 005
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
30
|
+
|
|
31
|
+
Time O(n) · Space O(1), one pass.
|
|
32
|
+
|
|
33
|
+
</details>
|
|
34
|
+
|
|
35
|
+
## Follow-ups an interviewer might ask
|
|
36
|
+
|
|
37
|
+
- What if you may make as many trades as you like? (LeetCode 122)
|
|
38
|
+
- What if you must also return the buy and sell days?
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { maxProfit } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('005 best time to buy and sell stock', () => {
|
|
6
|
+
it('finds the best single trade', () => {
|
|
7
|
+
expect(maxProfit([7, 1, 5, 3, 6, 4])).toBe(5)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('returns 0 when prices only fall', () => {
|
|
11
|
+
expect(maxProfit([7, 6, 4, 3, 1])).toBe(0)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('returns 0 for a single day', () => {
|
|
15
|
+
expect(maxProfit([5])).toBe(0)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('does not sell before buying', () => {
|
|
19
|
+
// The highest price (9) comes before the lowest (1).
|
|
20
|
+
expect(maxProfit([9, 2, 4, 1, 3])).toBe(2)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('uses a new low that appears later', () => {
|
|
24
|
+
expect(maxProfit([3, 8, 1, 7])).toBe(6)
|
|
25
|
+
expect(maxProfit([2, 4, 1])).toBe(2)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('handles flat prices', () => {
|
|
29
|
+
expect(maxProfit([3, 3, 3])).toBe(0)
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('005 performance', () => {
|
|
34
|
+
it('handles 100,000 days in under 200 ms (O(n²) is too slow here)', () => {
|
|
35
|
+
const prices = Array.from({ length: 100_000 }, (_, i) => 100_000 - i)
|
|
36
|
+
const ms = timeMs(() => expect(maxProfit(prices)).toBe(0))
|
|
37
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# 006 · Valid Parentheses
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: stack · Target: 15 min · NeetCode 150 · LeetCode 20
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
The string `s` contains only the characters `(`, `)`, `{`, `}`, `[` and `]`. Return `true` if the brackets are valid:
|
|
8
|
+
|
|
9
|
+
- every opening bracket is closed by the same type of bracket, and
|
|
10
|
+
- brackets close in the right order (the most recently opened one closes first).
|
|
11
|
+
|
|
12
|
+
## Examples
|
|
13
|
+
|
|
14
|
+
| `s` | Output |
|
|
15
|
+
|---|---|
|
|
16
|
+
| `"()"` | `true` |
|
|
17
|
+
| `"()[]{}"` | `true` |
|
|
18
|
+
| `"(]"` | `false` |
|
|
19
|
+
| `"([)]"` | `false` |
|
|
20
|
+
| `"{[]}"` | `true` |
|
|
21
|
+
|
|
22
|
+
## Constraints
|
|
23
|
+
|
|
24
|
+
- `1 <= s.length <= 10^4` (the performance test uses 100,000)
|
|
25
|
+
- `s` contains only bracket characters.
|
|
26
|
+
|
|
27
|
+
## Run it
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm watch 006
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
34
|
+
|
|
35
|
+
Time O(n) · Space O(n).
|
|
36
|
+
|
|
37
|
+
</details>
|
|
38
|
+
|
|
39
|
+
## Follow-ups an interviewer might ask
|
|
40
|
+
|
|
41
|
+
- If there were only one bracket type, could you use O(1) space?
|
|
42
|
+
- How would you report the position of the first error?
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { timeMs } from '../../lib/perf'
|
|
3
|
+
import { isValid } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('006 valid parentheses', () => {
|
|
6
|
+
it('accepts simple pairs', () => {
|
|
7
|
+
expect(isValid('()')).toBe(true)
|
|
8
|
+
expect(isValid('()[]{}')).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('accepts nesting', () => {
|
|
12
|
+
expect(isValid('{[]}')).toBe(true)
|
|
13
|
+
expect(isValid('([{}])[]')).toBe(true)
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('rejects mismatched types', () => {
|
|
17
|
+
expect(isValid('(]')).toBe(false)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('rejects wrong closing order', () => {
|
|
21
|
+
expect(isValid('([)]')).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('rejects unclosed openers', () => {
|
|
25
|
+
expect(isValid('(')).toBe(false)
|
|
26
|
+
expect(isValid('(()')).toBe(false)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('rejects a closer with nothing open', () => {
|
|
30
|
+
expect(isValid(']')).toBe(false)
|
|
31
|
+
expect(isValid('())')).toBe(false)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
describe('006 performance', () => {
|
|
36
|
+
it('handles 100,000 nested brackets in under 200 ms', () => {
|
|
37
|
+
const s = '('.repeat(50_000) + ')'.repeat(50_000)
|
|
38
|
+
const ms = timeMs(() => expect(isValid(s)).toBe(true))
|
|
39
|
+
expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 007 · Binary Search
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: binary search · Target: 10 min · NeetCode 150 · LeetCode 704
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
`nums` is sorted in ascending order with no duplicates. Return the index of `target`, or `-1` if it isn't there.
|
|
8
|
+
|
|
9
|
+
Your solution must read O(log n) elements. The test counts every element you read, so a linear scan (or `indexOf`) won't pass.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| `nums` | `target` | Output |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `[-1, 0, 3, 5, 9, 12]` | `9` | `4` |
|
|
16
|
+
| `[-1, 0, 3, 5, 9, 12]` | `2` | `-1` |
|
|
17
|
+
|
|
18
|
+
## Constraints
|
|
19
|
+
|
|
20
|
+
- `1 <= nums.length <= 10^4` (the read-count test uses 1,000,000 and allows up to 100 reads)
|
|
21
|
+
- All values are distinct and sorted ascending.
|
|
22
|
+
|
|
23
|
+
## Run it
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pnpm watch 007
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
30
|
+
|
|
31
|
+
Time O(log n) · Space O(1), iterative.
|
|
32
|
+
|
|
33
|
+
</details>
|
|
34
|
+
|
|
35
|
+
## Follow-ups an interviewer might ask
|
|
36
|
+
|
|
37
|
+
- Return the index where `target` would be inserted. (LeetCode 35)
|
|
38
|
+
- What changes if there are duplicates and you need the first occurrence?
|
|
39
|
+
- Why `lo + Math.floor((hi - lo) / 2)` instead of `(lo + hi) / 2` in other languages?
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { search } from './solution'
|
|
3
|
+
|
|
4
|
+
/** Wraps an array so every element read is counted. */
|
|
5
|
+
function counted(values: readonly number[]): { nums: readonly number[]; reads: () => number } {
|
|
6
|
+
let reads = 0
|
|
7
|
+
const nums = new Proxy(values, {
|
|
8
|
+
get(target, prop, receiver) {
|
|
9
|
+
if (typeof prop === 'string' && /^\d+$/.test(prop)) reads++
|
|
10
|
+
return Reflect.get(target, prop, receiver)
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
return { nums, reads: () => reads }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('007 binary search', () => {
|
|
17
|
+
it('finds a value', () => {
|
|
18
|
+
expect(search([-1, 0, 3, 5, 9, 12], 9)).toBe(4)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('returns -1 when the value is missing', () => {
|
|
22
|
+
expect(search([-1, 0, 3, 5, 9, 12], 2)).toBe(-1)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('finds the first and last elements', () => {
|
|
26
|
+
expect(search([1, 3, 5, 7], 1)).toBe(0)
|
|
27
|
+
expect(search([1, 3, 5, 7], 7)).toBe(3)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('handles a single element', () => {
|
|
31
|
+
expect(search([5], 5)).toBe(0)
|
|
32
|
+
expect(search([5], -5)).toBe(-1)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('returns -1 for targets outside the range', () => {
|
|
36
|
+
expect(search([2, 4, 6], 1)).toBe(-1)
|
|
37
|
+
expect(search([2, 4, 6], 7)).toBe(-1)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('finds every element of a two-element array', () => {
|
|
41
|
+
expect(search([1, 2], 1)).toBe(0)
|
|
42
|
+
expect(search([1, 2], 2)).toBe(1)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('007 read count', () => {
|
|
47
|
+
it('reads at most 100 elements of a 1,000,000-element array (a linear scan reads about a million)', () => {
|
|
48
|
+
const values = Array.from({ length: 1_000_000 }, (_, i) => i * 2)
|
|
49
|
+
for (const target of [0, 1_999_998, 777_776, 3, -4]) {
|
|
50
|
+
const { nums, reads } = counted(values)
|
|
51
|
+
const expected = target >= 0 && target % 2 === 0 && target <= 1_999_998 ? target / 2 : -1
|
|
52
|
+
expect(search(nums, target)).toBe(expected)
|
|
53
|
+
expect(reads(), `read ${reads()} elements looking for ${target}`).toBeLessThanOrEqual(100)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 008 · Reverse Linked List
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: linked list · Target: 10 min · NeetCode 150 · LeetCode 206
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given the `head` of a singly linked list, reverse it **in place** (relink the existing nodes; don't build a new list) and return the new head.
|
|
8
|
+
|
|
9
|
+
`ListNode` is in `lib/linked-list.ts` (`val`, `next`).
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| List | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `1 → 2 → 3 → 4 → 5` | `5 → 4 → 3 → 2 → 1` |
|
|
16
|
+
| `1 → 2` | `2 → 1` |
|
|
17
|
+
| empty | empty |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `0 <= length <= 5000`
|
|
22
|
+
- `-5000 <= val <= 5000`
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 008
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Iterative: Time O(n) · Space O(1). Recursive: Time O(n) · Space O(n) for the call stack. Be ready to write both.
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- Now do it the other way (recursive if you went iterative, and vice versa).
|
|
39
|
+
- Reverse only positions `left` to `right`. (LeetCode 92)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { listFrom, listToArray } from '../../lib/linked-list'
|
|
3
|
+
import { reverseList } from './solution'
|
|
4
|
+
|
|
5
|
+
const reversed = (values: number[]): number[] => listToArray(reverseList(listFrom(values)))
|
|
6
|
+
|
|
7
|
+
describe('008 reverse linked list', () => {
|
|
8
|
+
it('reverses five nodes', () => {
|
|
9
|
+
expect(reversed([1, 2, 3, 4, 5])).toEqual([5, 4, 3, 2, 1])
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('reverses two nodes', () => {
|
|
13
|
+
expect(reversed([1, 2])).toEqual([2, 1])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('handles an empty list', () => {
|
|
17
|
+
expect(reverseList(null)).toBeNull()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('handles a single node', () => {
|
|
21
|
+
expect(reversed([7])).toEqual([7])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('leaves no cycle behind (the old head must point to null)', () => {
|
|
25
|
+
const head = listFrom([1, 2, 3])
|
|
26
|
+
const newHead = reverseList(head)
|
|
27
|
+
expect(head?.next).toBeNull()
|
|
28
|
+
expect(listToArray(newHead, 10)).toEqual([3, 2, 1])
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('reuses the original nodes instead of copying them', () => {
|
|
32
|
+
const head = listFrom([1, 2, 3])
|
|
33
|
+
const tail = head?.next?.next
|
|
34
|
+
expect(reverseList(head)).toBe(tail)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('handles 5,000 nodes', () => {
|
|
38
|
+
const values = Array.from({ length: 5000 }, (_, i) => i)
|
|
39
|
+
expect(reversed(values)).toEqual([...values].reverse())
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# 009 · Merge Two Sorted Lists
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: linked list · Target: 15 min · NeetCode 150 · LeetCode 21
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
You get the heads of two linked lists, each sorted in ascending order. Merge them into one sorted list by relinking their existing nodes (don't create copies) and return the head of the merged list.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| `list1` | `list2` | Output |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `1 → 2 → 4` | `1 → 3 → 4` | `1 → 1 → 2 → 3 → 4 → 4` |
|
|
14
|
+
| empty | empty | empty |
|
|
15
|
+
| empty | `0` | `0` |
|
|
16
|
+
|
|
17
|
+
## Constraints
|
|
18
|
+
|
|
19
|
+
- Each list has `0` to `50` nodes.
|
|
20
|
+
- `-100 <= val <= 100`, and each list is sorted ascending.
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 009
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
Time O(n + m) · Space O(1) iterative (O(n + m) stack if recursive).
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- Merge `k` sorted lists. (LeetCode 23, a Level 3 problem in this ladder)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { ListNode, listFrom, listToArray } from '../../lib/linked-list'
|
|
3
|
+
import { mergeTwoLists } from './solution'
|
|
4
|
+
|
|
5
|
+
const merged = (a: number[], b: number[]): number[] => listToArray(mergeTwoLists(listFrom(a), listFrom(b)))
|
|
6
|
+
|
|
7
|
+
function nodesOf(head: ListNode | null): Set<ListNode> {
|
|
8
|
+
const nodes = new Set<ListNode>()
|
|
9
|
+
for (let n = head; n; n = n.next) nodes.add(n)
|
|
10
|
+
return nodes
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('009 merge two sorted lists', () => {
|
|
14
|
+
it('merges interleaved lists', () => {
|
|
15
|
+
expect(merged([1, 2, 4], [1, 3, 4])).toEqual([1, 1, 2, 3, 4, 4])
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('handles two empty lists', () => {
|
|
19
|
+
expect(mergeTwoLists(null, null)).toBeNull()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('handles one empty list', () => {
|
|
23
|
+
expect(merged([], [0])).toEqual([0])
|
|
24
|
+
expect(merged([2, 5], [])).toEqual([2, 5])
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('handles lists of different lengths', () => {
|
|
28
|
+
expect(merged([1], [2, 3, 4, 5])).toEqual([1, 2, 3, 4, 5])
|
|
29
|
+
expect(merged([5, 6, 7], [1])).toEqual([1, 5, 6, 7])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('handles negatives and duplicates', () => {
|
|
33
|
+
expect(merged([-10, -3, 0, 0], [-5, 0, 9])).toEqual([-10, -5, -3, 0, 0, 0, 9])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('relinks the original nodes instead of copying them', () => {
|
|
37
|
+
const a = listFrom([1, 4])
|
|
38
|
+
const b = listFrom([2, 3])
|
|
39
|
+
const original = new Set([...nodesOf(a), ...nodesOf(b)])
|
|
40
|
+
const result = nodesOf(mergeTwoLists(a, b))
|
|
41
|
+
expect(result).toEqual(original)
|
|
42
|
+
})
|
|
43
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# 010 · Linked List Cycle
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: linked list (fast and slow pointers) · Target: 10 min · NeetCode 150 · LeetCode 141
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given the `head` of a linked list, return `true` if following `next` pointers ever brings you back to a node you've already visited, and `false` if the list ends.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| List | Output |
|
|
12
|
+
|---|---|
|
|
13
|
+
| `3 → 2 → 0 → -4`, and `-4` points back to `2` | `true` |
|
|
14
|
+
| `1 → 2`, and `2` points back to `1` | `true` |
|
|
15
|
+
| `1` (no cycle) | `false` |
|
|
16
|
+
|
|
17
|
+
## Constraints
|
|
18
|
+
|
|
19
|
+
- `0 <= length <= 10^4`
|
|
20
|
+
- Don't modify the list.
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 010
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
A `Set` of visited nodes works in O(n) time and O(n) space. The expected answer is O(n) time and **O(1) space**. The tests can't measure memory, so this one is on your honour: get to O(1).
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- Return the node where the cycle starts. (LeetCode 142)
|
|
37
|
+
- Why are the fast and slow pointers guaranteed to meet?
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { listFrom, listToArray } from '../../lib/linked-list'
|
|
3
|
+
import { hasCycle } from './solution'
|
|
4
|
+
|
|
5
|
+
describe('010 linked list cycle', () => {
|
|
6
|
+
it('finds a cycle back to the middle', () => {
|
|
7
|
+
expect(hasCycle(listFrom([3, 2, 0, -4], 1))).toBe(true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('finds a cycle back to the head', () => {
|
|
11
|
+
expect(hasCycle(listFrom([1, 2], 0))).toBe(true)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('finds a single node pointing to itself', () => {
|
|
15
|
+
expect(hasCycle(listFrom([1], 0))).toBe(true)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('returns false for lists without a cycle', () => {
|
|
19
|
+
expect(hasCycle(null)).toBe(false)
|
|
20
|
+
expect(hasCycle(listFrom([1]))).toBe(false)
|
|
21
|
+
expect(hasCycle(listFrom([1, 2, 3, 4, 5]))).toBe(false)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('is not fooled by repeated values', () => {
|
|
25
|
+
expect(hasCycle(listFrom([1, 1, 1, 1]))).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('handles 10,000 nodes with a cycle at the very end', () => {
|
|
29
|
+
const values = Array.from({ length: 10_000 }, (_, i) => i)
|
|
30
|
+
expect(hasCycle(listFrom(values, 9_999))).toBe(true)
|
|
31
|
+
expect(hasCycle(listFrom(values))).toBe(false)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('does not modify the list', () => {
|
|
35
|
+
const head = listFrom([1, 2, 3, 4])
|
|
36
|
+
hasCycle(head)
|
|
37
|
+
expect(listToArray(head, 10)).toEqual([1, 2, 3, 4])
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# 011 · Invert Binary Tree
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: trees (DFS) · Target: 10 min · NeetCode 150 · LeetCode 226
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Given the `root` of a binary tree, mirror it: every node's left and right children swap places, all the way down. Return the root.
|
|
8
|
+
|
|
9
|
+
`TreeNode` is in `lib/binary-tree.ts` (`val`, `left`, `right`). Trees in the examples are written in level order, with `null` for a missing child.
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
| Tree | Output |
|
|
14
|
+
|---|---|
|
|
15
|
+
| `[4, 2, 7, 1, 3, 6, 9]` | `[4, 7, 2, 9, 6, 3, 1]` |
|
|
16
|
+
| `[2, 1, 3]` | `[2, 3, 1]` |
|
|
17
|
+
| `[]` | `[]` |
|
|
18
|
+
|
|
19
|
+
## Constraints
|
|
20
|
+
|
|
21
|
+
- `0 <= nodes <= 100`
|
|
22
|
+
- `-100 <= val <= 100`
|
|
23
|
+
|
|
24
|
+
## Run it
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm watch 011
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
31
|
+
|
|
32
|
+
Time O(n) · Space O(h), where h is the tree height (the recursion stack).
|
|
33
|
+
|
|
34
|
+
</details>
|
|
35
|
+
|
|
36
|
+
## Follow-ups an interviewer might ask
|
|
37
|
+
|
|
38
|
+
- Do it iteratively with a queue or stack.
|
|
39
|
+
- What's h for a balanced tree, and for the worst case?
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { treeFrom, treeToArray } from '../../lib/binary-tree'
|
|
3
|
+
import { invertTree } from './solution'
|
|
4
|
+
|
|
5
|
+
const inverted = (levelOrder: (number | null)[]): (number | null)[] => treeToArray(invertTree(treeFrom(levelOrder)))
|
|
6
|
+
|
|
7
|
+
describe('011 invert binary tree', () => {
|
|
8
|
+
it('mirrors a full tree', () => {
|
|
9
|
+
expect(inverted([4, 2, 7, 1, 3, 6, 9])).toEqual([4, 7, 2, 9, 6, 3, 1])
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it('mirrors a small tree', () => {
|
|
13
|
+
expect(inverted([2, 1, 3])).toEqual([2, 3, 1])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('handles an empty tree', () => {
|
|
17
|
+
expect(invertTree(null)).toBeNull()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('handles a single node', () => {
|
|
21
|
+
expect(inverted([1])).toEqual([1])
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('mirrors a lopsided tree', () => {
|
|
25
|
+
expect(inverted([1, 2, null, 3])).toEqual([1, null, 2, null, 3])
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('returns the same root node', () => {
|
|
29
|
+
const root = treeFrom([1, 2, 3])
|
|
30
|
+
expect(invertTree(root)).toBe(root)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# 012 · Maximum Depth of Binary Tree
|
|
2
|
+
|
|
3
|
+
**Level 1 · Easy** · Pattern: trees (DFS/BFS) · Target: 10 min · NeetCode 150 · LeetCode 104
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
Return the number of nodes on the longest path from the `root` down to a leaf. An empty tree has depth `0`.
|
|
8
|
+
|
|
9
|
+
## Examples
|
|
10
|
+
|
|
11
|
+
| Tree | Output |
|
|
12
|
+
|---|---|
|
|
13
|
+
| `[3, 9, 20, null, null, 15, 7]` | `3` |
|
|
14
|
+
| `[1, null, 2]` | `2` |
|
|
15
|
+
| `[]` | `0` |
|
|
16
|
+
|
|
17
|
+
## Constraints
|
|
18
|
+
|
|
19
|
+
- `0 <= nodes <= 10^4`
|
|
20
|
+
- `-100 <= val <= 100`
|
|
21
|
+
|
|
22
|
+
## Run it
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm watch 012
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
<details><summary>Target complexity (open after your first attempt)</summary>
|
|
29
|
+
|
|
30
|
+
Time O(n) · Space O(h) recursive DFS, or O(w) for BFS (w = widest level).
|
|
31
|
+
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
## Follow-ups an interviewer might ask
|
|
35
|
+
|
|
36
|
+
- Solve it three ways: recursive DFS, iterative DFS with a stack, BFS by levels.
|
|
37
|
+
- Minimum depth instead? (LeetCode 111: careful with nodes that have one child)
|