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.
Files changed (285) hide show
  1. package/README.md +59 -2
  2. package/content/CLAUDE.md +131 -0
  3. package/content/LADDER.md +166 -0
  4. package/content/behavioral/QUESTIONS.md +93 -0
  5. package/content/behavioral/REVIEW.md +86 -0
  6. package/content/design/D01-short-links/README.md +20 -0
  7. package/content/design/D02-rate-limiter/README.md +21 -0
  8. package/content/design/D03-news-feed/README.md +20 -0
  9. package/content/design/D04-chat-messaging/README.md +20 -0
  10. package/content/design/D05-notification-service/README.md +20 -0
  11. package/content/design/D06-typeahead-search/README.md +20 -0
  12. package/content/design/D07-web-crawler/README.md +20 -0
  13. package/content/design/D08-file-sync/README.md +20 -0
  14. package/content/design/D09-ride-matching/README.md +20 -0
  15. package/content/design/D10-job-scheduler/README.md +20 -0
  16. package/content/design/D11-metrics-alerting/README.md +20 -0
  17. package/content/design/D12-payments-ledger/README.md +20 -0
  18. package/content/design/REVIEW.md +94 -0
  19. package/content/lib/binary-tree.ts +69 -0
  20. package/content/lib/graph.ts +50 -0
  21. package/content/lib/linked-list.ts +37 -0
  22. package/content/lib/perf.ts +9 -0
  23. package/content/problems/001-contains-duplicate/README.md +37 -0
  24. package/content/problems/001-contains-duplicate/solution.test.ts +38 -0
  25. package/content/problems/002-valid-anagram/README.md +36 -0
  26. package/content/problems/002-valid-anagram/solution.test.ts +38 -0
  27. package/content/problems/003-two-sum/README.md +40 -0
  28. package/content/problems/003-two-sum/solution.test.ts +40 -0
  29. package/content/problems/004-valid-palindrome/README.md +39 -0
  30. package/content/problems/004-valid-palindrome/solution.test.ts +38 -0
  31. package/content/problems/005-best-time-to-buy-and-sell-stock/README.md +38 -0
  32. package/content/problems/005-best-time-to-buy-and-sell-stock/solution.test.ts +39 -0
  33. package/content/problems/006-valid-parentheses/README.md +42 -0
  34. package/content/problems/006-valid-parentheses/solution.test.ts +41 -0
  35. package/content/problems/007-binary-search/README.md +39 -0
  36. package/content/problems/007-binary-search/solution.test.ts +56 -0
  37. package/content/problems/008-reverse-linked-list/README.md +39 -0
  38. package/content/problems/008-reverse-linked-list/solution.test.ts +41 -0
  39. package/content/problems/009-merge-two-sorted-lists/README.md +36 -0
  40. package/content/problems/009-merge-two-sorted-lists/solution.test.ts +43 -0
  41. package/content/problems/010-linked-list-cycle/README.md +37 -0
  42. package/content/problems/010-linked-list-cycle/solution.test.ts +39 -0
  43. package/content/problems/011-invert-binary-tree/README.md +39 -0
  44. package/content/problems/011-invert-binary-tree/solution.test.ts +32 -0
  45. package/content/problems/012-maximum-depth-of-binary-tree/README.md +37 -0
  46. package/content/problems/012-maximum-depth-of-binary-tree/solution.test.ts +29 -0
  47. package/content/problems/013-same-tree/README.md +37 -0
  48. package/content/problems/013-same-tree/solution.test.ts +38 -0
  49. package/content/problems/014-balanced-binary-tree/README.md +37 -0
  50. package/content/problems/014-balanced-binary-tree/solution.test.ts +38 -0
  51. package/content/problems/015-climbing-stairs/README.md +35 -0
  52. package/content/problems/015-climbing-stairs/solution.test.ts +27 -0
  53. package/content/problems/016-diameter-of-binary-tree/README.md +38 -0
  54. package/content/problems/016-diameter-of-binary-tree/solution.test.ts +63 -0
  55. package/content/problems/017-subtree-of-another-tree/README.md +42 -0
  56. package/content/problems/017-subtree-of-another-tree/solution.test.ts +61 -0
  57. package/content/problems/018-kth-largest-element-in-a-stream/README.md +49 -0
  58. package/content/problems/018-kth-largest-element-in-a-stream/solution.test.ts +104 -0
  59. package/content/problems/019-last-stone-weight/README.md +44 -0
  60. package/content/problems/019-last-stone-weight/solution.test.ts +67 -0
  61. package/content/problems/020-min-cost-climbing-stairs/README.md +40 -0
  62. package/content/problems/020-min-cost-climbing-stairs/solution.test.ts +44 -0
  63. package/content/problems/021-single-number/README.md +41 -0
  64. package/content/problems/021-single-number/solution.test.ts +46 -0
  65. package/content/problems/022-missing-number/README.md +40 -0
  66. package/content/problems/022-missing-number/solution.test.ts +44 -0
  67. package/content/problems/101-group-anagrams/README.md +41 -0
  68. package/content/problems/101-group-anagrams/solution.test.ts +83 -0
  69. package/content/problems/102-top-k-frequent-elements/README.md +44 -0
  70. package/content/problems/102-top-k-frequent-elements/solution.test.ts +59 -0
  71. package/content/problems/103-product-of-array-except-self/README.md +43 -0
  72. package/content/problems/103-product-of-array-except-self/solution.test.ts +59 -0
  73. package/content/problems/104-longest-consecutive-sequence/README.md +40 -0
  74. package/content/problems/104-longest-consecutive-sequence/solution.test.ts +62 -0
  75. package/content/problems/105-encode-and-decode-strings/README.md +47 -0
  76. package/content/problems/105-encode-and-decode-strings/solution.test.ts +50 -0
  77. package/content/problems/106-two-sum-ii-input-array-is-sorted/README.md +43 -0
  78. package/content/problems/106-two-sum-ii-input-array-is-sorted/solution.test.ts +47 -0
  79. package/content/problems/107-three-sum/README.md +42 -0
  80. package/content/problems/107-three-sum/solution.test.ts +79 -0
  81. package/content/problems/108-container-with-most-water/README.md +40 -0
  82. package/content/problems/108-container-with-most-water/solution.test.ts +53 -0
  83. package/content/problems/109-longest-substring-without-repeating-characters/README.md +40 -0
  84. package/content/problems/109-longest-substring-without-repeating-characters/solution.test.ts +56 -0
  85. package/content/problems/110-longest-repeating-character-replacement/README.md +41 -0
  86. package/content/problems/110-longest-repeating-character-replacement/solution.test.ts +51 -0
  87. package/content/problems/111-permutation-in-string/README.md +38 -0
  88. package/content/problems/111-permutation-in-string/solution.test.ts +54 -0
  89. package/content/problems/112-min-stack/README.md +51 -0
  90. package/content/problems/112-min-stack/solution.test.ts +123 -0
  91. package/content/problems/113-daily-temperatures/README.md +40 -0
  92. package/content/problems/113-daily-temperatures/solution.test.ts +56 -0
  93. package/content/problems/114-evaluate-reverse-polish-notation/README.md +44 -0
  94. package/content/problems/114-evaluate-reverse-polish-notation/solution.test.ts +56 -0
  95. package/content/problems/115-generate-parentheses/README.md +38 -0
  96. package/content/problems/115-generate-parentheses/solution.test.ts +59 -0
  97. package/content/problems/116-koko-eating-bananas/README.md +41 -0
  98. package/content/problems/116-koko-eating-bananas/solution.test.ts +53 -0
  99. package/content/problems/117-find-minimum-in-rotated-sorted-array/README.md +41 -0
  100. package/content/problems/117-find-minimum-in-rotated-sorted-array/solution.test.ts +69 -0
  101. package/content/problems/118-search-in-rotated-sorted-array/README.md +41 -0
  102. package/content/problems/118-search-in-rotated-sorted-array/solution.test.ts +95 -0
  103. package/content/problems/119-time-based-key-value-store/README.md +50 -0
  104. package/content/problems/119-time-based-key-value-store/solution.test.ts +107 -0
  105. package/content/problems/120-reorder-list/README.md +43 -0
  106. package/content/problems/120-reorder-list/solution.test.ts +83 -0
  107. package/content/problems/121-remove-nth-node-from-end-of-list/README.md +40 -0
  108. package/content/problems/121-remove-nth-node-from-end-of-list/solution.test.ts +55 -0
  109. package/content/problems/122-lru-cache/README.md +55 -0
  110. package/content/problems/122-lru-cache/solution.test.ts +152 -0
  111. package/content/problems/123-binary-tree-level-order-traversal/README.md +40 -0
  112. package/content/problems/123-binary-tree-level-order-traversal/solution.test.ts +42 -0
  113. package/content/problems/124-binary-tree-right-side-view/README.md +40 -0
  114. package/content/problems/124-binary-tree-right-side-view/solution.test.ts +56 -0
  115. package/content/problems/125-validate-binary-search-tree/README.md +46 -0
  116. package/content/problems/125-validate-binary-search-tree/solution.test.ts +78 -0
  117. package/content/problems/126-kth-smallest-element-in-a-bst/README.md +39 -0
  118. package/content/problems/126-kth-smallest-element-in-a-bst/solution.test.ts +61 -0
  119. package/content/problems/127-lowest-common-ancestor-of-a-bst/README.md +46 -0
  120. package/content/problems/127-lowest-common-ancestor-of-a-bst/solution.test.ts +106 -0
  121. package/content/problems/128-kth-largest-element-in-an-array/README.md +42 -0
  122. package/content/problems/128-kth-largest-element-in-an-array/solution.test.ts +67 -0
  123. package/content/problems/129-k-closest-points-to-origin/README.md +43 -0
  124. package/content/problems/129-k-closest-points-to-origin/solution.test.ts +74 -0
  125. package/content/problems/130-number-of-islands/README.md +44 -0
  126. package/content/problems/130-number-of-islands/solution.test.ts +69 -0
  127. package/content/problems/131-course-schedule/README.md +42 -0
  128. package/content/problems/131-course-schedule/solution.test.ts +82 -0
  129. package/content/problems/132-rotting-oranges/README.md +46 -0
  130. package/content/problems/132-rotting-oranges/solution.test.ts +74 -0
  131. package/content/problems/133-clone-graph/README.md +42 -0
  132. package/content/problems/133-clone-graph/solution.test.ts +58 -0
  133. package/content/problems/134-house-robber/README.md +38 -0
  134. package/content/problems/134-house-robber/solution.test.ts +45 -0
  135. package/content/problems/135-coin-change/README.md +40 -0
  136. package/content/problems/135-coin-change/solution.test.ts +45 -0
  137. package/content/problems/136-longest-increasing-subsequence/README.md +41 -0
  138. package/content/problems/136-longest-increasing-subsequence/solution.test.ts +53 -0
  139. package/content/problems/137-word-break/README.md +39 -0
  140. package/content/problems/137-word-break/solution.test.ts +49 -0
  141. package/content/problems/138-merge-intervals/README.md +40 -0
  142. package/content/problems/138-merge-intervals/solution.test.ts +84 -0
  143. package/content/problems/139-insert-interval/README.md +40 -0
  144. package/content/problems/139-insert-interval/solution.test.ts +75 -0
  145. package/content/problems/201-valid-sudoku/README.md +63 -0
  146. package/content/problems/201-valid-sudoku/solution.test.ts +104 -0
  147. package/content/problems/202-car-fleet/README.md +46 -0
  148. package/content/problems/202-car-fleet/solution.test.ts +85 -0
  149. package/content/problems/203-search-a-2d-matrix/README.md +47 -0
  150. package/content/problems/203-search-a-2d-matrix/solution.test.ts +108 -0
  151. package/content/problems/204-copy-list-with-random-pointer/README.md +46 -0
  152. package/content/problems/204-copy-list-with-random-pointer/solution.test.ts +120 -0
  153. package/content/problems/205-add-two-numbers/README.md +45 -0
  154. package/content/problems/205-add-two-numbers/solution.test.ts +74 -0
  155. package/content/problems/206-find-the-duplicate-number/README.md +47 -0
  156. package/content/problems/206-find-the-duplicate-number/solution.test.ts +72 -0
  157. package/content/problems/207-count-good-nodes/README.md +44 -0
  158. package/content/problems/207-count-good-nodes/solution.test.ts +63 -0
  159. package/content/problems/208-construct-tree-from-preorder-and-inorder/README.md +52 -0
  160. package/content/problems/208-construct-tree-from-preorder-and-inorder/solution.test.ts +126 -0
  161. package/content/problems/209-implement-trie/README.md +52 -0
  162. package/content/problems/209-implement-trie/solution.test.ts +138 -0
  163. package/content/problems/210-design-add-and-search-words/README.md +50 -0
  164. package/content/problems/210-design-add-and-search-words/solution.test.ts +127 -0
  165. package/content/problems/211-task-scheduler/README.md +47 -0
  166. package/content/problems/211-task-scheduler/solution.test.ts +64 -0
  167. package/content/problems/212-design-twitter/README.md +63 -0
  168. package/content/problems/212-design-twitter/solution.test.ts +137 -0
  169. package/content/problems/213-subsets/README.md +42 -0
  170. package/content/problems/213-subsets/solution.test.ts +63 -0
  171. package/content/problems/214-combination-sum/README.md +44 -0
  172. package/content/problems/214-combination-sum/solution.test.ts +75 -0
  173. package/content/problems/215-permutations/README.md +42 -0
  174. package/content/problems/215-permutations/solution.test.ts +70 -0
  175. package/content/problems/216-word-search/README.md +50 -0
  176. package/content/problems/216-word-search/solution.test.ts +86 -0
  177. package/content/problems/217-letter-combinations-of-a-phone-number/README.md +53 -0
  178. package/content/problems/217-letter-combinations-of-a-phone-number/solution.test.ts +57 -0
  179. package/content/problems/218-palindrome-partitioning/README.md +42 -0
  180. package/content/problems/218-palindrome-partitioning/solution.test.ts +77 -0
  181. package/content/problems/219-max-area-of-island/README.md +43 -0
  182. package/content/problems/219-max-area-of-island/solution.test.ts +78 -0
  183. package/content/problems/220-pacific-atlantic-water-flow/README.md +43 -0
  184. package/content/problems/220-pacific-atlantic-water-flow/solution.test.ts +86 -0
  185. package/content/problems/221-course-schedule-ii/README.md +42 -0
  186. package/content/problems/221-course-schedule-ii/solution.test.ts +107 -0
  187. package/content/problems/222-graph-valid-tree/README.md +42 -0
  188. package/content/problems/222-graph-valid-tree/solution.test.ts +75 -0
  189. package/content/problems/223-redundant-connection/README.md +42 -0
  190. package/content/problems/223-redundant-connection/solution.test.ts +68 -0
  191. package/content/problems/224-network-delay-time/README.md +44 -0
  192. package/content/problems/224-network-delay-time/solution.test.ts +70 -0
  193. package/content/problems/225-cheapest-flights-within-k-stops/README.md +45 -0
  194. package/content/problems/225-cheapest-flights-within-k-stops/solution.test.ts +85 -0
  195. package/content/problems/226-house-robber-ii/README.md +41 -0
  196. package/content/problems/226-house-robber-ii/solution.test.ts +51 -0
  197. package/content/problems/227-longest-palindromic-substring/README.md +41 -0
  198. package/content/problems/227-longest-palindromic-substring/solution.test.ts +69 -0
  199. package/content/problems/228-decode-ways/README.md +44 -0
  200. package/content/problems/228-decode-ways/solution.test.ts +50 -0
  201. package/content/problems/229-maximum-product-subarray/README.md +40 -0
  202. package/content/problems/229-maximum-product-subarray/solution.test.ts +55 -0
  203. package/content/problems/230-partition-equal-subset-sum/README.md +39 -0
  204. package/content/problems/230-partition-equal-subset-sum/solution.test.ts +54 -0
  205. package/content/problems/231-unique-paths/README.md +41 -0
  206. package/content/problems/231-unique-paths/solution.test.ts +45 -0
  207. package/content/problems/232-longest-common-subsequence/README.md +41 -0
  208. package/content/problems/232-longest-common-subsequence/solution.test.ts +48 -0
  209. package/content/problems/233-coin-change-ii/README.md +44 -0
  210. package/content/problems/233-coin-change-ii/solution.test.ts +45 -0
  211. package/content/problems/234-edit-distance/README.md +45 -0
  212. package/content/problems/234-edit-distance/solution.test.ts +48 -0
  213. package/content/problems/235-maximum-subarray/README.md +40 -0
  214. package/content/problems/235-maximum-subarray/solution.test.ts +63 -0
  215. package/content/problems/236-jump-game/README.md +40 -0
  216. package/content/problems/236-jump-game/solution.test.ts +68 -0
  217. package/content/problems/237-gas-station/README.md +42 -0
  218. package/content/problems/237-gas-station/solution.test.ts +56 -0
  219. package/content/problems/238-partition-labels/README.md +40 -0
  220. package/content/problems/238-partition-labels/solution.test.ts +54 -0
  221. package/content/problems/239-non-overlapping-intervals/README.md +40 -0
  222. package/content/problems/239-non-overlapping-intervals/solution.test.ts +61 -0
  223. package/content/problems/240-meeting-rooms-ii/README.md +42 -0
  224. package/content/problems/240-meeting-rooms-ii/solution.test.ts +65 -0
  225. package/content/problems/241-rotate-image/README.md +41 -0
  226. package/content/problems/241-rotate-image/solution.test.ts +72 -0
  227. package/content/problems/242-spiral-matrix/README.md +40 -0
  228. package/content/problems/242-spiral-matrix/solution.test.ts +90 -0
  229. package/content/problems/243-set-matrix-zeroes/README.md +44 -0
  230. package/content/problems/243-set-matrix-zeroes/solution.test.ts +153 -0
  231. package/content/problems/301-trapping-rain-water/README.md +42 -0
  232. package/content/problems/301-trapping-rain-water/solution.test.ts +63 -0
  233. package/content/problems/302-minimum-window-substring/README.md +43 -0
  234. package/content/problems/302-minimum-window-substring/solution.test.ts +56 -0
  235. package/content/problems/303-sliding-window-maximum/README.md +42 -0
  236. package/content/problems/303-sliding-window-maximum/solution.test.ts +63 -0
  237. package/content/problems/304-largest-rectangle-in-histogram/README.md +42 -0
  238. package/content/problems/304-largest-rectangle-in-histogram/solution.test.ts +57 -0
  239. package/content/problems/305-median-of-two-sorted-arrays/README.md +42 -0
  240. package/content/problems/305-median-of-two-sorted-arrays/solution.test.ts +84 -0
  241. package/content/problems/306-merge-k-sorted-lists/README.md +42 -0
  242. package/content/problems/306-merge-k-sorted-lists/solution.test.ts +72 -0
  243. package/content/problems/307-reverse-nodes-in-k-group/README.md +42 -0
  244. package/content/problems/307-reverse-nodes-in-k-group/solution.test.ts +86 -0
  245. package/content/problems/308-binary-tree-maximum-path-sum/README.md +44 -0
  246. package/content/problems/308-binary-tree-maximum-path-sum/solution.test.ts +91 -0
  247. package/content/problems/309-serialize-and-deserialize-binary-tree/README.md +50 -0
  248. package/content/problems/309-serialize-and-deserialize-binary-tree/solution.test.ts +120 -0
  249. package/content/problems/310-find-median-from-data-stream/README.md +47 -0
  250. package/content/problems/310-find-median-from-data-stream/solution.test.ts +102 -0
  251. package/content/problems/311-word-search-ii/README.md +51 -0
  252. package/content/problems/311-word-search-ii/solution.test.ts +144 -0
  253. package/content/problems/312-n-queens/README.md +41 -0
  254. package/content/problems/312-n-queens/solution.test.ts +91 -0
  255. package/content/problems/313-word-ladder/README.md +42 -0
  256. package/content/problems/313-word-ladder/solution.test.ts +85 -0
  257. package/content/problems/314-alien-dictionary/README.md +43 -0
  258. package/content/problems/314-alien-dictionary/solution.test.ts +127 -0
  259. package/content/problems/315-longest-increasing-path-in-a-matrix/README.md +42 -0
  260. package/content/problems/315-longest-increasing-path-in-a-matrix/solution.test.ts +118 -0
  261. package/content/problems/316-swim-in-rising-water/README.md +44 -0
  262. package/content/problems/316-swim-in-rising-water/solution.test.ts +138 -0
  263. package/content/product/P01-tool-lending/README.md +28 -0
  264. package/content/product/P02-city-bike-share/README.md +28 -0
  265. package/content/product/P03-caring-from-afar/README.md +29 -0
  266. package/content/product/P04-first-garden/README.md +28 -0
  267. package/content/product/P05-save-for-later/README.md +28 -0
  268. package/content/product/P06-ship-or-hold/README.md +28 -0
  269. package/content/product/P07-photo-backup-goals/README.md +28 -0
  270. package/content/product/P08-quarter-roadmap/README.md +28 -0
  271. package/content/product/P09-grocer-delivery/README.md +29 -0
  272. package/content/product/P10-notes-for-work/README.md +28 -0
  273. package/content/product/P11-fitness-coaching/README.md +28 -0
  274. package/content/product/P12-low-price-rival/README.md +28 -0
  275. package/content/product/P13-coffee-cups/README.md +28 -0
  276. package/content/product/P14-bike-repair-market/README.md +27 -0
  277. package/content/product/P15-scooter-rides/README.md +27 -0
  278. package/content/product/P16-school-lunch-boxes/README.md +28 -0
  279. package/content/product/P17-weekly-users-drop/README.md +28 -0
  280. package/content/product/P18-ride-cancellations/README.md +28 -0
  281. package/content/product/P19-signups-up-revenue-flat/README.md +28 -0
  282. package/content/product/P20-longer-sessions/README.md +28 -0
  283. package/content/product/REVIEW.md +107 -0
  284. package/dist/cli.mjs +4462 -0
  285. package/package.json +38 -4
@@ -0,0 +1,29 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { rightSpine, treeFrom } from '../../lib/binary-tree'
3
+ import { maxDepth } from './solution'
4
+
5
+ describe('012 maximum depth of binary tree', () => {
6
+ it('measures a balanced tree', () => {
7
+ expect(maxDepth(treeFrom([3, 9, 20, null, null, 15, 7]))).toBe(3)
8
+ })
9
+
10
+ it('measures a right-leaning tree', () => {
11
+ expect(maxDepth(treeFrom([1, null, 2]))).toBe(2)
12
+ })
13
+
14
+ it('returns 0 for an empty tree', () => {
15
+ expect(maxDepth(null)).toBe(0)
16
+ })
17
+
18
+ it('returns 1 for a single node', () => {
19
+ expect(maxDepth(treeFrom([0]))).toBe(1)
20
+ })
21
+
22
+ it('follows the deeper side even when it is on the left', () => {
23
+ expect(maxDepth(treeFrom([1, 2, 3, 4, null, null, null, 5]))).toBe(4)
24
+ })
25
+
26
+ it('handles a 5,000-node chain', () => {
27
+ expect(maxDepth(rightSpine(5_000))).toBe(5_000)
28
+ })
29
+ })
@@ -0,0 +1,37 @@
1
+ # 013 · Same Tree
2
+
3
+ **Level 1 · Easy** · Pattern: trees (DFS) · Target: 10 min · NeetCode 150 · LeetCode 100
4
+
5
+ ## Problem
6
+
7
+ Given the roots `p` and `q` of two binary trees, return `true` if they have exactly the same shape and the same value at every position.
8
+
9
+ ## Examples
10
+
11
+ | `p` | `q` | Output |
12
+ |---|---|---|
13
+ | `[1, 2, 3]` | `[1, 2, 3]` | `true` |
14
+ | `[1, 2]` | `[1, null, 2]` | `false` (same values, different shape) |
15
+ | `[1, 2, 1]` | `[1, 1, 2]` | `false` |
16
+
17
+ ## Constraints
18
+
19
+ - `0 <= nodes in each tree <= 100`
20
+ - `-10^4 <= val <= 10^4`
21
+
22
+ ## Run it
23
+
24
+ ```bash
25
+ pnpm watch 013
26
+ ```
27
+
28
+ <details><summary>Target complexity (open after your first attempt)</summary>
29
+
30
+ Time O(min(n, m)) · Space O(h).
31
+
32
+ </details>
33
+
34
+ ## Follow-ups an interviewer might ask
35
+
36
+ - Is one tree a subtree of the other? (LeetCode 572, in the optional Level 1b)
37
+ - Is a single tree symmetric around its centre? (LeetCode 101)
@@ -0,0 +1,38 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { treeFrom } from '../../lib/binary-tree'
3
+ import { isSameTree } from './solution'
4
+
5
+ type Level = (number | null)[]
6
+ const same = (a: Level, b: Level): boolean => isSameTree(treeFrom(a), treeFrom(b))
7
+
8
+ describe('013 same tree', () => {
9
+ it('accepts identical trees', () => {
10
+ expect(same([1, 2, 3], [1, 2, 3])).toBe(true)
11
+ })
12
+
13
+ it('rejects the same values in a different shape', () => {
14
+ expect(same([1, 2], [1, null, 2])).toBe(false)
15
+ })
16
+
17
+ it('rejects swapped values', () => {
18
+ expect(same([1, 2, 1], [1, 1, 2])).toBe(false)
19
+ })
20
+
21
+ it('handles empty trees', () => {
22
+ expect(same([], [])).toBe(true)
23
+ expect(same([], [1])).toBe(false)
24
+ expect(same([1], [])).toBe(false)
25
+ })
26
+
27
+ it('compares a difference deep in the tree', () => {
28
+ expect(same([5, 4, 8, 11, null, 13, 4, 7, 2], [5, 4, 8, 11, null, 13, 4, 7, 2])).toBe(true)
29
+ expect(same([5, 4, 8, 11, null, 13, 4, 7, 2], [5, 4, 8, 11, null, 13, 4, 7, 3])).toBe(false)
30
+ })
31
+
32
+ it('treats zero as a real value, not a missing node', () => {
33
+ expect(same([0], [0])).toBe(true)
34
+ expect(same([0], [1])).toBe(false)
35
+ expect(same([1, 0], [1])).toBe(false)
36
+ expect(same([1, 0], [1, 2])).toBe(false)
37
+ })
38
+ })
@@ -0,0 +1,37 @@
1
+ # 014 · Balanced Binary Tree
2
+
3
+ **Level 1 · Easy** · Pattern: trees (DFS, bottom-up) · Target: 15 min · NeetCode 150 · LeetCode 110
4
+
5
+ ## Problem
6
+
7
+ A binary tree is height-balanced if, at **every** node, the heights of the left and right subtrees differ by at most 1. Return `true` if the given tree is height-balanced.
8
+
9
+ ## Examples
10
+
11
+ | Tree | Output |
12
+ |---|---|
13
+ | `[3, 9, 20, null, null, 15, 7]` | `true` |
14
+ | `[1, 2, 2, 3, 3, null, null, 4, 4]` | `false` |
15
+ | `[]` | `true` |
16
+
17
+ ## Constraints
18
+
19
+ - `0 <= nodes <= 5000`
20
+ - `-10^4 <= val <= 10^4`
21
+
22
+ ## Run it
23
+
24
+ ```bash
25
+ pnpm watch 014
26
+ ```
27
+
28
+ <details><summary>Target complexity (open after your first attempt)</summary>
29
+
30
+ Time O(n) · Space O(h). The obvious top-down version (call `height()` at every node) is O(n log n) on balanced trees and O(n²) in the worst case. The tests don't time this one, so judge yourself: aim for a single bottom-up pass.
31
+
32
+ </details>
33
+
34
+ ## Follow-ups an interviewer might ask
35
+
36
+ - Why is the top-down version slower? Walk through its cost on a balanced tree.
37
+ - Diameter of a binary tree (LeetCode 543) uses the same bottom-up trick.
@@ -0,0 +1,38 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { rightSpine, treeFrom } from '../../lib/binary-tree'
3
+ import { isBalanced } from './solution'
4
+
5
+ const balanced = (levelOrder: (number | null)[]): boolean => isBalanced(treeFrom(levelOrder))
6
+
7
+ describe('014 balanced binary tree', () => {
8
+ it('accepts a balanced tree', () => {
9
+ expect(balanced([3, 9, 20, null, null, 15, 7])).toBe(true)
10
+ })
11
+
12
+ it('rejects a tree that leans too far left', () => {
13
+ expect(balanced([1, 2, 2, 3, 3, null, null, 4, 4])).toBe(false)
14
+ })
15
+
16
+ it('accepts empty and single-node trees', () => {
17
+ expect(isBalanced(null)).toBe(true)
18
+ expect(balanced([1])).toBe(true)
19
+ })
20
+
21
+ it('accepts a height difference of exactly 1', () => {
22
+ expect(balanced([1, 2])).toBe(true)
23
+ expect(balanced([1, 2, 3, 4])).toBe(true)
24
+ })
25
+
26
+ it('rejects a difference of 2', () => {
27
+ expect(balanced([1, null, 2, null, 3])).toBe(false)
28
+ })
29
+
30
+ it('checks every node, not just the root', () => {
31
+ // Root subtrees both have height 3, but the nodes below them don't balance.
32
+ expect(balanced([1, 2, 2, 3, null, null, 3, 4, null, null, 4])).toBe(false)
33
+ })
34
+
35
+ it('rejects a long chain', () => {
36
+ expect(isBalanced(rightSpine(3_000))).toBe(false)
37
+ })
38
+ })
@@ -0,0 +1,35 @@
1
+ # 015 · Climbing Stairs
2
+
3
+ **Level 1 · Easy** · Pattern: 1-D dynamic programming · Target: 15 min · NeetCode 150 · LeetCode 70
4
+
5
+ ## Problem
6
+
7
+ A staircase has `n` steps. Each move, you climb either 1 or 2 steps. Return how many different sequences of moves get you exactly to the top.
8
+
9
+ ## Examples
10
+
11
+ | `n` | Output | Ways |
12
+ |---|---|---|
13
+ | `2` | `2` | `1+1`, `2` |
14
+ | `3` | `3` | `1+1+1`, `1+2`, `2+1` |
15
+
16
+ ## Constraints
17
+
18
+ - `1 <= n <= 45`
19
+
20
+ ## Run it
21
+
22
+ ```bash
23
+ pnpm watch 015
24
+ ```
25
+
26
+ <details><summary>Target complexity (open after your first attempt)</summary>
27
+
28
+ Time O(n) · Space O(1), keeping only the last two values. Plain recursion is O(2^n) and fails the performance test; memoisation gets you O(n) time and O(n) space.
29
+
30
+ </details>
31
+
32
+ ## Follow-ups an interviewer might ask
33
+
34
+ - What if each step has a cost and you want the cheapest climb? (LeetCode 746)
35
+ - What if you may climb 1, 2 or 3 steps? Or any step size from a given set?
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { climbStairs } from './solution'
4
+
5
+ describe('015 climbing stairs', () => {
6
+ it('handles the smallest staircases', () => {
7
+ expect(climbStairs(1)).toBe(1)
8
+ expect(climbStairs(2)).toBe(2)
9
+ })
10
+
11
+ it('counts three steps', () => {
12
+ expect(climbStairs(3)).toBe(3)
13
+ })
14
+
15
+ it('counts a few more', () => {
16
+ expect(climbStairs(4)).toBe(5)
17
+ expect(climbStairs(5)).toBe(8)
18
+ expect(climbStairs(10)).toBe(89)
19
+ })
20
+ })
21
+
22
+ describe('015 performance', () => {
23
+ it('computes n = 45 in under 50 ms (plain recursion takes seconds)', () => {
24
+ const ms = timeMs(() => expect(climbStairs(45)).toBe(1_836_311_903))
25
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(50)
26
+ })
27
+ })
@@ -0,0 +1,38 @@
1
+ # 016 · Diameter of Binary Tree
2
+
3
+ **Level 1 · Easy** · Pattern: trees (DFS, bottom-up) · Target: 15 min · NeetCode 150 · LeetCode 543
4
+
5
+ ## Problem
6
+
7
+ Given the `root` of a binary tree, return the length of the longest path between any two nodes, counted in **edges**. The path may or may not go through `root`. An empty tree, or a tree with one node, has length `0`.
8
+
9
+ ## Examples
10
+
11
+ | `root` | Output | Longest path |
12
+ |---|---|---|
13
+ | `[4, 7, 2, null, 9, 6]` | `4` | `9 → 7 → 4 → 2 → 6` |
14
+ | `[8, 3]` | `1` | `3 → 8` |
15
+ | `[5]` | `0` | |
16
+
17
+ ## Constraints
18
+
19
+ - `0 <= nodes <= 10^4`, `-100 <= val <= 100`
20
+ - The performance test uses about 100,000 nodes in a tree 3,000 levels deep.
21
+
22
+ ## Run it
23
+
24
+ ```bash
25
+ pnpm watch 016
26
+ ```
27
+
28
+ <details><summary>Target complexity (open after your first attempt)</summary>
29
+
30
+ Time O(n) · Space O(h), with one bottom-up pass that returns each subtree's height and records the best path on the way. Calling a separate `height()` at every node is O(n · h), which is O(n²) on a deep tree, and fails the performance test.
31
+
32
+ </details>
33
+
34
+ ## Follow-ups an interviewer might ask
35
+
36
+ - Why does the answer not always pass through the root? Draw a tree where it doesn't.
37
+ - Return the longest path's node values, not just its length.
38
+ - What if each edge had a weight (the diameter of a weighted tree)?
@@ -0,0 +1,63 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { TreeNode, rightSpine, treeFrom } from '../../lib/binary-tree'
3
+ import { timeMs } from '../../lib/perf'
4
+ import { diameterOfBinaryTree } from './solution'
5
+
6
+ const diameter = (levelOrder: (number | null)[]): number => diameterOfBinaryTree(treeFrom(levelOrder))
7
+
8
+ /** A right-leaning chain of `depth` nodes with a complete tree of `bushNodes` nodes hanging from its last node. */
9
+ function chainWithBush(depth: number, bushNodes: number): TreeNode {
10
+ const bush = treeFrom(Array.from({ length: bushNodes }, (_, i) => i % 100))
11
+ const root = new TreeNode(0)
12
+ let tail = root
13
+ for (let i = 1; i < depth; i++) {
14
+ tail.right = new TreeNode(i % 100)
15
+ tail = tail.right
16
+ }
17
+ tail.right = bush
18
+ return root
19
+ }
20
+
21
+ describe('016 diameter of binary tree', () => {
22
+ it('finds a path through the root', () => {
23
+ expect(diameter([4, 7, 2, null, 9, 6])).toBe(4)
24
+ })
25
+
26
+ it('handles two nodes', () => {
27
+ expect(diameter([8, 3])).toBe(1)
28
+ })
29
+
30
+ it('returns 0 for an empty tree or a single node', () => {
31
+ expect(diameterOfBinaryTree(null)).toBe(0)
32
+ expect(diameter([5])).toBe(0)
33
+ })
34
+
35
+ it('counts edges, not nodes', () => {
36
+ expect(diameter([1, 2, 3])).toBe(2)
37
+ })
38
+
39
+ it('finds a path that does not pass through the root', () => {
40
+ // The root's right child is a single leaf; the long path bends at node 2.
41
+ expect(diameter([1, 2, 3, 4, 5, null, null, 6, null, null, 7, 8, null, null, 9])).toBe(6)
42
+ })
43
+
44
+ it('handles a one-sided chain', () => {
45
+ expect(diameter([1, null, 2, null, 3, null, 4])).toBe(3)
46
+ expect(diameterOfBinaryTree(rightSpine(2_000))).toBe(1_999)
47
+ })
48
+
49
+ it('handles negatives and zero values', () => {
50
+ expect(diameter([0, -1, -2, -3, null, null, -4])).toBe(4)
51
+ })
52
+ })
53
+
54
+ describe('016 performance', () => {
55
+ it('handles 100,000 nodes, 3,000 deep, in under 200 ms (computing heights again at every node is too slow here)', () => {
56
+ const depth = 3_000
57
+ const bushNodes = 100_000
58
+ const root = chainWithBush(depth, bushNodes)
59
+ // The bush has 17 levels, so its deepest leaf is depth + 16 edges below the root.
60
+ const ms = timeMs(() => expect(diameterOfBinaryTree(root)).toBe(depth + 16))
61
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
62
+ })
63
+ })
@@ -0,0 +1,42 @@
1
+ # 017 · Subtree of Another Tree
2
+
3
+ **Level 1 · Easy** · Pattern: trees (DFS) · Target: 15 min · NeetCode 150 · LeetCode 572
4
+
5
+ ## Problem
6
+
7
+ Given the roots of two binary trees, `root` and `subRoot`, return `true` if some node in `root` is the top of a subtree that matches `subRoot` exactly: the same shape and the same value at every position, all the way down to the leaves.
8
+
9
+ A match must include **everything** below that node. Matching only the upper part of a subtree doesn't count.
10
+
11
+ An empty `subRoot` matches (every tree contains the empty tree). A non-empty `subRoot` never matches inside an empty `root`.
12
+
13
+ ## Examples
14
+
15
+ | `root` | `subRoot` | Output |
16
+ |---|---|---|
17
+ | `[6, 2, 9, 1, 4]` | `[2, 1, 4]` | `true` |
18
+ | `[6, 2, 9, 1, 4, null, null, null, null, 3]` | `[2, 1, 4]` | `false` (the 4 has a child that `subRoot` lacks) |
19
+ | `[7, 7, 7]` | `[7]` | `true` |
20
+
21
+ ## Constraints
22
+
23
+ - `0 <= nodes in root <= 2000`, `0 <= nodes in subRoot <= 1000`
24
+ - `-10^4 <= val <= 10^4`
25
+
26
+ ## Run it
27
+
28
+ ```bash
29
+ pnpm watch 017
30
+ ```
31
+
32
+ <details><summary>Target complexity (open after your first attempt)</summary>
33
+
34
+ Checking for a match at every node of `root` is O(m · n) in the worst case (m and n are the two tree sizes) and is what most interviewers expect; it passes. Space O(h). There is no performance test: the O(m + n) approaches below are follow-ups, not requirements.
35
+
36
+ </details>
37
+
38
+ ## Follow-ups an interviewer might ask
39
+
40
+ - Can you get O(m + n)? (Serialise both trees with markers for missing children, then do a linear-time substring search such as KMP, or hash every subtree.)
41
+ - Why do the missing-child markers matter when you serialise? Find two different trees that would serialise the same without them.
42
+ - How does this relate to 013 Same Tree?
@@ -0,0 +1,61 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { TreeNode, rightSpine, treeFrom } from '../../lib/binary-tree'
3
+ import { isSubtree } from './solution'
4
+
5
+ type Level = (number | null)[]
6
+ const contains = (root: Level, subRoot: Level): boolean => isSubtree(treeFrom(root), treeFrom(subRoot))
7
+
8
+ describe('017 subtree of another tree', () => {
9
+ it('finds a matching subtree', () => {
10
+ expect(contains([6, 2, 9, 1, 4], [2, 1, 4])).toBe(true)
11
+ })
12
+
13
+ it('rejects a match that is missing a descendant', () => {
14
+ expect(contains([6, 2, 9, 1, 4, null, null, null, null, 3], [2, 1, 4])).toBe(false)
15
+ })
16
+
17
+ it('finds a single leaf among repeated values', () => {
18
+ expect(contains([7, 7, 7], [7])).toBe(true)
19
+ })
20
+
21
+ it('matches the whole tree', () => {
22
+ expect(contains([1, 2, 3], [1, 2, 3])).toBe(true)
23
+ })
24
+
25
+ it('handles empty trees', () => {
26
+ expect(contains([1, 2], [])).toBe(true)
27
+ expect(contains([], [])).toBe(true)
28
+ expect(contains([], [1])).toBe(false)
29
+ })
30
+
31
+ it('rejects the same values in a different shape', () => {
32
+ expect(contains([3, 4, 5, 1, 2], [4, null, 1])).toBe(false)
33
+ expect(contains([3, 4, 5, 1], [4, null, 1])).toBe(false)
34
+ expect(contains([3, 4, 5, null, 1], [4, null, 1])).toBe(true)
35
+ })
36
+
37
+ it('keeps looking after a near miss higher up', () => {
38
+ // The two upper 1s have the wrong children; only the lowest 1 (with left child 3) matches.
39
+ expect(contains([1, 1, null, 1, 2, 3], [1, 3])).toBe(true)
40
+ })
41
+
42
+ it('does not confuse values that share digits', () => {
43
+ expect(contains([12], [2])).toBe(false)
44
+ expect(contains([1, 23], [3])).toBe(false)
45
+ expect(contains([-2, 2], [2])).toBe(true)
46
+ expect(contains([2, -2], [-2])).toBe(true)
47
+ expect(contains([-12], [2])).toBe(false)
48
+ })
49
+
50
+ it('treats zero as a real value', () => {
51
+ expect(contains([1, 0], [0])).toBe(true)
52
+ expect(contains([1, 0], [0, 0])).toBe(false)
53
+ })
54
+
55
+ it('finds the bottom of a long chain', () => {
56
+ const root = rightSpine(2_000)
57
+ const subRoot = new TreeNode(1_999, null, new TreeNode(2_000))
58
+ expect(isSubtree(root, subRoot)).toBe(true)
59
+ expect(isSubtree(root, new TreeNode(1_999))).toBe(false)
60
+ })
61
+ })
@@ -0,0 +1,49 @@
1
+ # 018 · Kth Largest Element in a Stream
2
+
3
+ **Level 1 · Easy** · Pattern: heap / priority queue · Target: 15 min · NeetCode 150 · LeetCode 703
4
+
5
+ ## Problem
6
+
7
+ Scores arrive one at a time, and after each one you must report the `k`-th highest score seen so far. Export a class `KthLargest` with:
8
+
9
+ - `constructor(k, nums)`: start with the scores in `nums` already seen. `nums` may hold fewer than `k` scores, or more.
10
+ - `add(val)`: record the new score `val`, then return the `k`-th largest of everything seen so far.
11
+
12
+ The `k`-th largest is by position in sorted order, so duplicates count separately: among `[5, 5, 2]` the 1st and 2nd largest are both `5`. Every call to `add` happens when at least `k` scores have been seen (counting the new one). Don't modify `nums`.
13
+
14
+ ## Examples
15
+
16
+ `k = 2`, `nums = [5, 1, 8]`, then:
17
+
18
+ | Call | Returns | Scores seen |
19
+ |---|---|---|
20
+ | `add(3)` | `5` | `1, 3, 5, 8` |
21
+ | `add(9)` | `8` | `1, 3, 5, 8, 9` |
22
+ | `add(2)` | `8` | `1, 2, 3, 5, 8, 9` |
23
+ | `add(8)` | `8` | `1, 2, 3, 5, 8, 8, 9` |
24
+ | `add(10)` | `9` | `1, 2, 3, 5, 8, 8, 9, 10` |
25
+
26
+ ## Constraints
27
+
28
+ - `1 <= k`, `0 <= nums.length`, and at least `k` scores have been seen whenever `add` is called.
29
+ - `-10^4 <= val <= 10^4` in the examples. The performance test starts with 20,000 scores and `k = 2,000`, then makes 10,000 calls to `add`.
30
+
31
+ ## Run it
32
+
33
+ ```bash
34
+ pnpm watch 018
35
+ ```
36
+
37
+ <details><summary>Target complexity (open after your first attempt)</summary>
38
+
39
+ Keep only the `k` largest scores in a min-heap: O(n log k) for the constructor, O(log k) per `add`, O(k) space. Sorting everything again on every `add` is O(n log n) per call and fails the performance test. A sorted array of the `k` largest, with a binary search and an insert, is O(k) per `add`; it passes here, but explain why the heap scales better.
40
+
41
+ TypeScript has no built-in priority queue, so for the heap version write a small binary heap yourself (an array plus sift-up and sift-down).
42
+
43
+ </details>
44
+
45
+ ## Follow-ups an interviewer might ask
46
+
47
+ - Why a **min**-heap of size `k`, when you want large values?
48
+ - What if scores could also be removed from the stream?
49
+ - How would you report the median instead of the `k`-th largest? (Two heaps; LeetCode 295.)
@@ -0,0 +1,104 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { KthLargest } from './solution'
4
+
5
+ /** Deterministic shuffle of 0..n-1 (Fisher-Yates with a seeded MINSTD generator), so test data is the same every run. */
6
+ function shuffledRange(n: number, seed: number): number[] {
7
+ const out = Array.from({ length: n }, (_, i) => i)
8
+ let state = seed
9
+ for (let i = n - 1; i > 0; i--) {
10
+ state = (state * 48_271) % 2_147_483_647
11
+ const j = state % (i + 1)
12
+ ;[out[i], out[j]] = [out[j]!, out[i]!]
13
+ }
14
+ return out
15
+ }
16
+
17
+ describe('018 kth largest element in a stream', () => {
18
+ it('runs the README example', () => {
19
+ const tracker = new KthLargest(2, [5, 1, 8])
20
+ expect(tracker.add(3)).toBe(5)
21
+ expect(tracker.add(9)).toBe(8)
22
+ expect(tracker.add(2)).toBe(8)
23
+ expect(tracker.add(8)).toBe(8)
24
+ expect(tracker.add(10)).toBe(9)
25
+ })
26
+
27
+ it('with k = 1, tracks the maximum', () => {
28
+ const tracker = new KthLargest(1, [4])
29
+ expect(tracker.add(2)).toBe(4)
30
+ expect(tracker.add(7)).toBe(7)
31
+ expect(tracker.add(7)).toBe(7)
32
+ })
33
+
34
+ it('starts with fewer than k numbers', () => {
35
+ const empty = new KthLargest(1, [])
36
+ expect(empty.add(-3)).toBe(-3)
37
+ expect(empty.add(-5)).toBe(-3)
38
+
39
+ const short = new KthLargest(3, [6, 2])
40
+ expect(short.add(4)).toBe(2)
41
+ expect(short.add(1)).toBe(2)
42
+ expect(short.add(5)).toBe(4)
43
+ })
44
+
45
+ it('starts with more than k numbers', () => {
46
+ const tracker = new KthLargest(3, [9, 1, 7, 3, 5, 2])
47
+ expect(tracker.add(0)).toBe(5)
48
+ expect(tracker.add(6)).toBe(6)
49
+ expect(tracker.add(10)).toBe(7)
50
+ })
51
+
52
+ it('counts duplicates separately', () => {
53
+ const tracker = new KthLargest(3, [4, 4])
54
+ expect(tracker.add(4)).toBe(4)
55
+ expect(tracker.add(1)).toBe(4)
56
+ expect(tracker.add(5)).toBe(4)
57
+ expect(tracker.add(5)).toBe(4)
58
+ expect(tracker.add(5)).toBe(5)
59
+ })
60
+
61
+ it('compares numbers, not strings, and handles negatives', () => {
62
+ const tracker = new KthLargest(2, [100, 9, -20])
63
+ expect(tracker.add(25)).toBe(25)
64
+ expect(tracker.add(-1)).toBe(25)
65
+ expect(tracker.add(1_000)).toBe(100)
66
+ })
67
+
68
+ it('does not modify the starting array', () => {
69
+ const nums = [3, 1, 2]
70
+ const tracker = new KthLargest(2, nums)
71
+ tracker.add(5)
72
+ expect(nums).toEqual([3, 1, 2])
73
+ })
74
+
75
+ it('keeps separate trackers independent', () => {
76
+ const a = new KthLargest(1, [1])
77
+ const b = new KthLargest(1, [100])
78
+ expect(a.add(2)).toBe(2)
79
+ expect(b.add(3)).toBe(100)
80
+ })
81
+ })
82
+
83
+ describe('018 performance', () => {
84
+ it('starts with 20,000 numbers, k = 2,000, then handles 10,000 adds in under 200 ms (sorting on every add is too slow here)', () => {
85
+ const start = 20_000
86
+ const k = 2_000
87
+ const adds = 10_000
88
+ const nums = shuffledRange(start, 7)
89
+ let mismatch = ''
90
+ const ms = timeMs(() => {
91
+ const tracker = new KthLargest(k, nums)
92
+ let bigAdded = 0
93
+ for (let i = 0; i < adds; i++) {
94
+ // Even steps add a new largest value; odd steps add a value below everything.
95
+ const val = i % 2 === 0 ? start + bigAdded++ : -1 - i
96
+ const expected = start + bigAdded - k
97
+ const got = tracker.add(val)
98
+ if (got !== expected && mismatch === '') mismatch = `add(${val}) returned ${got}, expected ${expected}`
99
+ }
100
+ })
101
+ expect(mismatch).toBe('')
102
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
103
+ })
104
+ })
@@ -0,0 +1,44 @@
1
+ # 019 · Last Stone Weight
2
+
3
+ **Level 1 · Easy** · Pattern: heap / priority queue · Target: 15 min · NeetCode 150 · LeetCode 1046
4
+
5
+ ## Problem
6
+
7
+ You have a pile of rocks with positive integer weights `stones`. Repeat this until at most one rock is left:
8
+
9
+ - Take the two heaviest rocks, weighing `a` and `b` with `a >= b`.
10
+ - If `a === b`, both are destroyed. Otherwise the lighter one is destroyed and the heavier one is chipped down to weight `a - b`, going back on the pile.
11
+
12
+ Return the weight of the last rock, or `0` if none are left. Don't modify `stones`.
13
+
14
+ ## Examples
15
+
16
+ | `stones` | Output | Rounds |
17
+ |---|---|---|
18
+ | `[3, 9, 4, 2]` | `0` | 9 vs 4 → 5; 5 vs 3 → 2; 2 vs 2 → none |
19
+ | `[10, 4]` | `6` | 10 vs 4 → 6 |
20
+ | `[7]` | `7` | nothing to smash |
21
+
22
+ ## Constraints
23
+
24
+ - `1 <= stones.length <= 30`, `1 <= stones[i] <= 1000` on LeetCode.
25
+ - The performance test smashes two piles of about 34,000 rocks, weighing up to 34,002, in under 100 ms.
26
+
27
+ ## Run it
28
+
29
+ ```bash
30
+ pnpm watch 019
31
+ ```
32
+
33
+ <details><summary>Target complexity (open after your first attempt)</summary>
34
+
35
+ Time O(n log n) · Space O(n), with a max-heap: each round removes two rocks and adds at most one. Scanning the whole pile for the two heaviest every round is O(n²) and fails the performance test; re-sorting every round is slower still. Keeping a sorted array and inserting each chipped rock with a binary search is also O(n²), because the insert shifts elements, and fails too.
36
+
37
+ TypeScript has no built-in priority queue, so write a small binary heap yourself (an array plus sift-up and sift-down).
38
+
39
+ </details>
40
+
41
+ ## Follow-ups an interviewer might ask
42
+
43
+ - Why is a heap faster than keeping the pile sorted, when both find the heaviest rock instantly?
44
+ - Variant: you may split the rocks into two groups and smash them in any order; what's the smallest possible last weight? (LeetCode 1049, a DP problem.)