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,68 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { canReachEnd } from './solution'
4
+
5
+ describe('236 jump game', () => {
6
+ it('reaches the end through a long jump in the middle', () => {
7
+ // 0 -> 1, then 1 can jump up to 3 steps, to 4, then 4 -> 5
8
+ expect(canReachEnd([1, 3, 0, 0, 2, 0])).toBe(true)
9
+ })
10
+
11
+ it('gets stuck on a zero that every path lands on', () => {
12
+ expect(canReachEnd([2, 0, 1, 0, 3])).toBe(false)
13
+ })
14
+
15
+ it('is already at the end with a single element', () => {
16
+ expect(canReachEnd([0])).toBe(true)
17
+ expect(canReachEnd([7])).toBe(true)
18
+ })
19
+
20
+ it('cannot leave a zero at the start', () => {
21
+ expect(canReachEnd([0, 1])).toBe(false)
22
+ })
23
+
24
+ it('may jump past the end', () => {
25
+ expect(canReachEnd([9, 0, 0])).toBe(true)
26
+ })
27
+
28
+ it('may take a shorter jump than the maximum', () => {
29
+ // From 0, jumping the full 2 lands on a zero; jumping 1 reaches index 1, which jumps over it.
30
+ expect(canReachEnd([2, 3, 0, 0, 1])).toBe(true)
31
+ })
32
+
33
+ it('does not need to stop on the last index exactly', () => {
34
+ expect(canReachEnd([1, 1, 1, 1])).toBe(true)
35
+ })
36
+
37
+ it('fails when every route stops on the same zero', () => {
38
+ expect(canReachEnd([1, 2, 1, 0, 6])).toBe(false)
39
+ })
40
+
41
+ it('ignores a zero in the last position', () => {
42
+ expect(canReachEnd([1, 2, 0])).toBe(true)
43
+ })
44
+
45
+ it('does not modify the input', () => {
46
+ const nums = [2, 0, 1]
47
+ canReachEnd(nums)
48
+ expect(nums).toEqual([2, 0, 1])
49
+ })
50
+ })
51
+
52
+ describe('236 performance', () => {
53
+ it('handles 120,000 positions in under 100 ms (marking every landing spot from every position, O(n²), is too slow here)', () => {
54
+ const n = 120_000
55
+ const wall = n / 2
56
+ // Every position before the wall can jump exactly as far as the wall and no further, and the wall is a 0.
57
+ const nums = Array.from({ length: n }, (_, i) => (i < wall ? wall - i : i === wall ? 0 : 1))
58
+ const ms = timeMs(() => expect(canReachEnd(nums)).toBe(false))
59
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(100)
60
+ })
61
+
62
+ it('handles 120,000 positions that do reach the end in under 100 ms', () => {
63
+ const n = 120_000
64
+ const nums = Array.from({ length: n }, (_, i) => (i % 2 === 0 ? 2 : 0))
65
+ const ms = timeMs(() => expect(canReachEnd(nums)).toBe(true))
66
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(100)
67
+ })
68
+ })
@@ -0,0 +1,42 @@
1
+ # 237 · Gas Station
2
+
3
+ **Level 2 · Medium** · Pattern: greedy (reset the start when the tank goes negative) · Target: 25 min · NeetCode 150 · LeetCode 134
4
+
5
+ ## Problem
6
+
7
+ Stations `0, 1, …, n - 1` sit around a circular road. At station `i` you can fill up `gas[i]` units of fuel, and driving from station `i` on to the next station (station `0` after the last one) burns `cost[i]` units. Your tank has no size limit and starts empty.
8
+
9
+ Pick a station to start from, fill up there, and drive all the way round, back to the same station, refuelling at each station you reach. The tank may hit exactly `0` on arrival, but it may never need to go below `0` during a leg.
10
+
11
+ Return the index of the station you can start from, or `-1` if no start works. When a start exists, the tests make sure there is exactly one. Don't modify `gas` or `cost`.
12
+
13
+ ## Examples
14
+
15
+ | `gas` | `cost` | Output |
16
+ |---|---|---|
17
+ | `[3, 1, 4]` | `[2, 3, 2]` | `2` (tank after each leg: 2, 3, 1) |
18
+ | `[1, 2]` | `[2, 2]` | `-1` |
19
+ | `[2, 1]` | `[1, 2]` | `0` (tank after each leg: 1, 0) |
20
+
21
+ ## Constraints
22
+
23
+ - `1 <= gas.length == cost.length <= 10^5` (the performance test uses 40,000)
24
+ - `0 <= gas[i], cost[i] <= 10^4`
25
+
26
+ ## Run it
27
+
28
+ ```bash
29
+ pnpm watch 237
30
+ ```
31
+
32
+ <details><summary>Target complexity (open after your first attempt)</summary>
33
+
34
+ Time O(n) · Space O(1). Simulating the trip from every station is O(n²); the performance test is built so most of those simulations run a long way before failing, and it rejects that.
35
+
36
+ </details>
37
+
38
+ ## Follow-ups an interviewer might ask
39
+
40
+ - Prove that if the total fuel is at least the total cost, some start always works.
41
+ - Why can you skip every station between a failed start and the point where it failed?
42
+ - Return every station that works, if the answer doesn't have to be unique.
@@ -0,0 +1,56 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { startingStation } from './solution'
4
+
5
+ describe('237 gas station', () => {
6
+ it('finds the only station that works', () => {
7
+ expect(startingStation([3, 1, 4], [2, 3, 2])).toBe(2)
8
+ })
9
+
10
+ it('returns -1 when there is not enough gas in total', () => {
11
+ expect(startingStation([1, 2], [2, 2])).toBe(-1)
12
+ })
13
+
14
+ it('handles a single station', () => {
15
+ expect(startingStation([5], [5])).toBe(0)
16
+ expect(startingStation([4], [1])).toBe(0)
17
+ expect(startingStation([2], [3])).toBe(-1)
18
+ })
19
+
20
+ it('allows arriving with an empty tank', () => {
21
+ expect(startingStation([2, 1], [1, 2])).toBe(0)
22
+ })
23
+
24
+ it('starts after a run of empty stations', () => {
25
+ expect(startingStation([0, 0, 7], [2, 2, 3])).toBe(2)
26
+ })
27
+
28
+ it('finds a start in the middle', () => {
29
+ // Starting at 1, the tank after each leg holds 1, 4, 3, 0. Every other start runs dry.
30
+ expect(startingStation([0, 2, 4, 1], [3, 1, 1, 2])).toBe(1)
31
+ })
32
+
33
+ it('returns -1 when the totals are close but short', () => {
34
+ expect(startingStation([3, 2, 4], [4, 3, 3])).toBe(-1)
35
+ })
36
+
37
+ it('does not modify the input', () => {
38
+ const gas = [3, 1, 4]
39
+ const cost = [2, 3, 2]
40
+ startingStation(gas, cost)
41
+ expect(gas).toEqual([3, 1, 4])
42
+ expect(cost).toEqual([2, 3, 2])
43
+ })
44
+ })
45
+
46
+ describe('237 performance', () => {
47
+ it('handles 40,000 stations in under 100 ms (simulating the trip from every station, O(n²), is too slow here)', () => {
48
+ const n = 40_000
49
+ // Every leg breaks even, except the leg out of n - 2, which needs 1 extra unit.
50
+ // Only station n - 1 has a spare unit, so it is the only start that works.
51
+ const gas = Array.from({ length: n }, (_, i) => (i === n - 1 ? 2 : 1))
52
+ const cost = Array.from({ length: n }, (_, i) => (i === n - 2 ? 2 : 1))
53
+ const ms = timeMs(() => expect(startingStation(gas, cost)).toBe(n - 1))
54
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(100)
55
+ })
56
+ })
@@ -0,0 +1,40 @@
1
+ # 238 · Partition Labels
2
+
3
+ **Level 2 · Medium** · Pattern: greedy (last occurrence of each letter) · Target: 25 min · NeetCode 150 · LeetCode 763
4
+
5
+ ## Problem
6
+
7
+ You get a string `s` of lowercase English letters. Cut it into as many consecutive pieces as you can, so that every letter appears in **one piece only** (all copies of a letter end up in the same piece).
8
+
9
+ Return the lengths of the pieces, in order from left to right. They always add up to `s.length`.
10
+
11
+ ## Examples
12
+
13
+ | `s` | Output |
14
+ |---|---|
15
+ | `"abacdcfeef"` | `[3, 3, 4]` (`"aba"`, `"cdc"`, `"feef"`) |
16
+ | `"xyz"` | `[1, 1, 1]` |
17
+ | `"abca"` | `[4]` |
18
+
19
+ ## Constraints
20
+
21
+ - `1 <= s.length <= 500` (the performance test uses 60,000)
22
+ - `s` contains only `a`–`z`.
23
+
24
+ ## Run it
25
+
26
+ ```bash
27
+ pnpm watch 238
28
+ ```
29
+
30
+ <details><summary>Target complexity (open after your first attempt)</summary>
31
+
32
+ Time O(n) · Space O(1) (one slot per letter, 26 at most, plus the output). Searching the string again for a letter's last position at every index is O(n²) in the worst case, and the performance test rejects it.
33
+
34
+ </details>
35
+
36
+ ## Follow-ups an interviewer might ask
37
+
38
+ - Return the pieces themselves instead of their lengths.
39
+ - Why is cutting as early as possible always safe? Could an early cut ever force fewer pieces later?
40
+ - How does this relate to merging overlapping intervals (one interval per letter, from its first to its last position)?
@@ -0,0 +1,54 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { partitionLabels } from './solution'
4
+
5
+ describe('238 partition labels', () => {
6
+ it('splits into three pieces', () => {
7
+ // "aba" | "cdc" | "feef"
8
+ expect(partitionLabels('abacdcfeef')).toEqual([3, 3, 4])
9
+ })
10
+
11
+ it('gives every letter its own piece when nothing repeats', () => {
12
+ expect(partitionLabels('xyz')).toEqual([1, 1, 1])
13
+ })
14
+
15
+ it('keeps the whole string together when the first letter is also the last', () => {
16
+ expect(partitionLabels('abca')).toEqual([4])
17
+ })
18
+
19
+ it('handles a single letter', () => {
20
+ expect(partitionLabels('q')).toEqual([1])
21
+ })
22
+
23
+ it('keeps a run of one repeated letter together', () => {
24
+ expect(partitionLabels('zzzz')).toEqual([4])
25
+ })
26
+
27
+ it('grows a piece when a later letter reaches further', () => {
28
+ // "a" ends at 4, but "b" inside it is fine; "d" stands alone.
29
+ expect(partitionLabels('abcbad')).toEqual([5, 1])
30
+ // "a" ends at 2, but "b" at 1 reaches to 5.
31
+ expect(partitionLabels('abaxxb')).toEqual([6])
32
+ })
33
+
34
+ it('handles pieces at both ends', () => {
35
+ expect(partitionLabels('mnnmopqrrq')).toEqual([4, 1, 1, 4])
36
+ })
37
+
38
+ it('lengths always add up to the string length', () => {
39
+ const s = 'thequickbrownfoxjumpsoverthelazydog'
40
+ const parts = partitionLabels(s)
41
+ expect(parts.reduce((a, b) => a + b, 0)).toBe(s.length)
42
+ })
43
+ })
44
+
45
+ describe('238 performance', () => {
46
+ it('handles 60,000 letters in under 100 ms (searching from every index for the last position of its letter, O(n²), is too slow here)', () => {
47
+ const half = 30_000
48
+ // The first half cycles through a..y, so it can't be split. The second half is all z's: one more piece.
49
+ const head = Array.from({ length: half }, (_, i) => String.fromCharCode(97 + (i % 25))).join('')
50
+ const s = head + 'z'.repeat(half)
51
+ const ms = timeMs(() => expect(partitionLabels(s)).toEqual([half, half]))
52
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(100)
53
+ })
54
+ })
@@ -0,0 +1,40 @@
1
+ # 239 · Non-overlapping Intervals
2
+
3
+ **Level 2 · Medium** · Pattern: intervals (greedy, sort by end) · Target: 25 min · NeetCode 150 · LeetCode 435
4
+
5
+ ## Problem
6
+
7
+ You get a list of `intervals`, each `[start, end]` with `start < end`. Remove as few of them as possible so that none of the rest overlap. Return how many you removed.
8
+
9
+ Intervals that only touch, like `[1, 3]` and `[3, 5]`, do **not** overlap. The input is in no particular order. Don't modify `intervals`.
10
+
11
+ ## Examples
12
+
13
+ | `intervals` | Output |
14
+ |---|---|
15
+ | `[[1, 4], [2, 3], [3, 6], [5, 7]]` | `2` (keep `[2, 3]` and `[3, 6]`, or `[2, 3]` and `[5, 7]`) |
16
+ | `[[0, 2], [2, 4], [4, 6]]` | `0` |
17
+ | `[[1, 5], [1, 5], [1, 5]]` | `2` |
18
+
19
+ ## Constraints
20
+
21
+ - `0 <= intervals.length <= 10^5` (the performance test uses 30,000)
22
+ - `-5 · 10^4 <= start < end <= 5 · 10^4`
23
+
24
+ ## Run it
25
+
26
+ ```bash
27
+ pnpm watch 239
28
+ ```
29
+
30
+ <details><summary>Target complexity (open after your first attempt)</summary>
31
+
32
+ Time O(n log n) for the sort, then one O(n) pass · Space O(n) for a sorted copy (O(log n) or O(1) extra if you could sort in place). Comparing every pair of intervals, for example a DP over the sorted list that looks back at every earlier interval, is O(n²) and the performance test rejects it.
33
+
34
+ </details>
35
+
36
+ ## Follow-ups an interviewer might ask
37
+
38
+ - Prove that keeping the interval that **ends** first is never a mistake.
39
+ - Sorting by start also works if you handle overlaps carefully. What must you keep when two overlap?
40
+ - How is this the same as scheduling the most meetings in one room?
@@ -0,0 +1,61 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { minRemovals } from './solution'
4
+
5
+ type Interval = readonly [number, number]
6
+
7
+ /** Deterministic reordering of 0..n-1, so the input isn't already sorted. 7,919 is prime and doesn't divide n. */
8
+ const scrambled = (n: number): number[] => Array.from({ length: n }, (_, i) => (i * 7_919) % n)
9
+
10
+ describe('239 non-overlapping intervals', () => {
11
+ it('removes two intervals', () => {
12
+ expect(minRemovals([[1, 4], [2, 3], [3, 6], [5, 7]])).toBe(2)
13
+ })
14
+
15
+ it('treats intervals that only touch as not overlapping', () => {
16
+ expect(minRemovals([[0, 2], [2, 4], [4, 6]])).toBe(0)
17
+ })
18
+
19
+ it('keeps one copy of identical intervals', () => {
20
+ expect(minRemovals([[1, 5], [1, 5], [1, 5]])).toBe(2)
21
+ })
22
+
23
+ it('handles empty input and a single interval', () => {
24
+ expect(minRemovals([])).toBe(0)
25
+ expect(minRemovals([[3, 8]])).toBe(0)
26
+ })
27
+
28
+ it('removes one long interval rather than the short ones inside it', () => {
29
+ expect(minRemovals([[1, 10], [2, 3], [4, 5], [6, 7]])).toBe(1)
30
+ })
31
+
32
+ it('handles unsorted input', () => {
33
+ expect(minRemovals([[6, 7], [1, 10], [4, 5], [2, 3]])).toBe(1)
34
+ expect(minRemovals([[5, 9], [0, 3], [2, 6]])).toBe(1)
35
+ })
36
+
37
+ it('handles negative values', () => {
38
+ expect(minRemovals([[-5, -1], [-3, 2], [0, 4]])).toBe(1)
39
+ })
40
+
41
+ it('handles a chain where each interval overlaps its neighbours', () => {
42
+ // Keep [0, 2], [2, 4], [4, 6]; remove [1, 3] and [3, 5].
43
+ expect(minRemovals([[0, 2], [1, 3], [2, 4], [3, 5], [4, 6]])).toBe(2)
44
+ })
45
+
46
+ it('does not modify the input', () => {
47
+ const intervals: Interval[] = [[3, 6], [1, 4], [5, 7]]
48
+ minRemovals(intervals)
49
+ expect(intervals).toEqual([[3, 6], [1, 4], [5, 7]])
50
+ })
51
+ })
52
+
53
+ describe('239 performance', () => {
54
+ it('handles 30,000 intervals in under 150 ms (comparing every pair of intervals, O(n²), is too slow here)', () => {
55
+ const n = 30_000
56
+ // [i, i + 2] for every i: keeping every second one is the best possible.
57
+ const intervals: Interval[] = scrambled(n).map((i): Interval => [i, i + 2])
58
+ const ms = timeMs(() => expect(minRemovals(intervals)).toBe(n / 2))
59
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(150)
60
+ })
61
+ })
@@ -0,0 +1,42 @@
1
+ # 240 · Meeting Rooms II
2
+
3
+ **Level 2 · Medium** · Pattern: intervals (sweep line, or a min-heap of end times) · Target: 25 min · NeetCode 150 · LeetCode 253
4
+
5
+ ## Problem
6
+
7
+ You get a list of `meetings`, each `[start, end]` with `start < end`. A meeting occupies a room from `start` up to, but not including, `end`, so a meeting that starts at the exact moment another ends can use the same room.
8
+
9
+ Return the smallest number of rooms needed to hold every meeting. No meetings need `0` rooms. The input is in no particular order. Don't modify `meetings`.
10
+
11
+ ## Examples
12
+
13
+ | `meetings` | Output |
14
+ |---|---|
15
+ | `[[3, 25], [6, 9], [12, 18]]` | `2` |
16
+ | `[[11, 14], [1, 6]]` | `1` |
17
+ | `[[1, 5], [5, 9]]` | `1` (the second starts as the first ends) |
18
+
19
+ ## Constraints
20
+
21
+ - `0 <= meetings.length <= 10^4` (the performance test uses 30,000)
22
+ - `0 <= start < end <= 10^6`
23
+
24
+ ## Run it
25
+
26
+ ```bash
27
+ pnpm watch 240
28
+ ```
29
+
30
+ <details><summary>Target complexity (open after your first attempt)</summary>
31
+
32
+ Time O(n log n) · Space O(n). Counting, for every meeting, how many others are running when it starts is O(n²), and the performance test rejects it.
33
+
34
+ TypeScript has no built-in priority queue. A min-heap of end times works if you write a small heap yourself; sorting the start and end times separately needs no heap at all. Both pass.
35
+
36
+ </details>
37
+
38
+ ## Follow-ups an interviewer might ask
39
+
40
+ - Assign each meeting to a specific room number, not just count the rooms.
41
+ - Can a single person attend every meeting? (Meeting Rooms, LeetCode 252.)
42
+ - The times are small integers (say, minutes in a day). Can you do better than sorting?
@@ -0,0 +1,65 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { minMeetingRooms } from './solution'
4
+
5
+ type Meeting = readonly [number, number]
6
+
7
+ /** Deterministic reordering of 0..n-1, so the input isn't already sorted. 7,919 is prime and doesn't divide n. */
8
+ const scrambled = (n: number): number[] => Array.from({ length: n }, (_, i) => (i * 7_919) % n)
9
+
10
+ describe('240 meeting rooms II', () => {
11
+ it('needs two rooms when two short meetings sit inside a long one', () => {
12
+ expect(minMeetingRooms([[3, 25], [6, 9], [12, 18]])).toBe(2)
13
+ })
14
+
15
+ it('needs one room when nothing overlaps', () => {
16
+ expect(minMeetingRooms([[11, 14], [1, 6]])).toBe(1)
17
+ })
18
+
19
+ it('lets a meeting start the moment another ends', () => {
20
+ expect(minMeetingRooms([[1, 5], [5, 9]])).toBe(1)
21
+ expect(minMeetingRooms([[5, 9], [1, 5], [9, 12]])).toBe(1)
22
+ })
23
+
24
+ it('needs a room per meeting when all of them overlap', () => {
25
+ expect(minMeetingRooms([[1, 10], [2, 9], [3, 8]])).toBe(3)
26
+ })
27
+
28
+ it('handles no meetings and a single meeting', () => {
29
+ expect(minMeetingRooms([])).toBe(0)
30
+ expect(minMeetingRooms([[3, 4]])).toBe(1)
31
+ })
32
+
33
+ it('counts identical meetings separately', () => {
34
+ expect(minMeetingRooms([[4, 6], [4, 6]])).toBe(2)
35
+ })
36
+
37
+ it('reuses rooms as meetings finish', () => {
38
+ expect(minMeetingRooms([[1, 3], [2, 5], [3, 7], [5, 8]])).toBe(2)
39
+ })
40
+
41
+ it('finds the busiest moment, not just the longest chain', () => {
42
+ // At time 6, [0, 7], [4, 9] and [6, 8] are all running.
43
+ expect(minMeetingRooms([[8, 12], [0, 7], [6, 8], [4, 9], [10, 11]])).toBe(3)
44
+ })
45
+
46
+ it('handles meetings starting at 0 and large times', () => {
47
+ expect(minMeetingRooms([[0, 1_000_000], [999_999, 1_000_000], [0, 1]])).toBe(2)
48
+ })
49
+
50
+ it('does not modify the input', () => {
51
+ const meetings: Meeting[] = [[6, 9], [3, 25], [12, 18]]
52
+ minMeetingRooms(meetings)
53
+ expect(meetings).toEqual([[6, 9], [3, 25], [12, 18]])
54
+ })
55
+ })
56
+
57
+ describe('240 performance', () => {
58
+ it('handles 30,000 meetings in under 150 ms (checking every meeting against every other, O(n²), is too slow here)', () => {
59
+ const n = 30_000
60
+ // Meeting i runs over [i, i + 50), so at most 50 run at once.
61
+ const meetings: Meeting[] = scrambled(n).map((i): Meeting => [i, i + 50])
62
+ const ms = timeMs(() => expect(minMeetingRooms(meetings)).toBe(50))
63
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(150)
64
+ })
65
+ })
@@ -0,0 +1,41 @@
1
+ # 241 · Rotate Image
2
+
3
+ **Level 2 · Medium** · Pattern: matrix (in-place transpose and reverse, or four-way swaps) · Target: 25 min · NeetCode 150 · LeetCode 48
4
+
5
+ ## Problem
6
+
7
+ `matrix` is an `n × n` grid of integers, such as the pixels of a square image. Turn it a quarter turn **clockwise**: the top row becomes the right-hand column, the right-hand column becomes the bottom row, and so on.
8
+
9
+ Do it **in place**: change the cells of the matrix you were given, using only O(1) extra memory (a few variables), not a second grid. The function returns nothing (`void`); the tests read the matrix they passed in.
10
+
11
+ ## Examples
12
+
13
+ | `matrix` | After the call |
14
+ |---|---|
15
+ | `[[1, 2], [3, 4]]` | `[[3, 1], [4, 2]]` |
16
+ | `[[10, 11, 12], [13, 14, 15], [16, 17, 18]]` | `[[16, 13, 10], [17, 14, 11], [18, 15, 12]]` |
17
+ | `[[7]]` | `[[7]]` |
18
+
19
+ ## Constraints
20
+
21
+ - `1 <= n <= 20`
22
+ - `-1000 <= matrix[r][c] <= 1000`
23
+ - The tests check the matrix itself, so returning a new rotated grid without changing `matrix` fails.
24
+
25
+ ## Run it
26
+
27
+ ```bash
28
+ pnpm watch 241
29
+ ```
30
+
31
+ <details><summary>Target complexity (open after your first attempt)</summary>
32
+
33
+ Time O(n²) (every cell has to move) · Space **O(1)**. Building a rotated copy and writing it back is O(n²) space and will pass the tests, because they can't measure memory. This one is on your honour: get to O(1) extra space.
34
+
35
+ </details>
36
+
37
+ ## Follow-ups an interviewer might ask
38
+
39
+ - Rotate counter-clockwise instead. What changes? And a half turn?
40
+ - How would you rotate a non-square `m × n` grid, and why can't it be done in place the same way?
41
+ - Rotate by `k` quarter turns, where `k` can be large or negative.
@@ -0,0 +1,72 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { rotate } from './solution'
3
+
4
+ /** Rotates `matrix` with the solution, checks nothing was returned, and gives back the same (changed) matrix. */
5
+ function rotated(matrix: number[][]): number[][] {
6
+ const result = rotate(matrix)
7
+ expect(result).toBeUndefined()
8
+ return matrix
9
+ }
10
+
11
+ /** An n × n grid where every cell holds a different value: row * 100 + column. */
12
+ const grid = (n: number): number[][] =>
13
+ Array.from({ length: n }, (_, r) => Array.from({ length: n }, (_, c) => r * 100 + c))
14
+
15
+ describe('241 rotate image', () => {
16
+ it('rotates a 2 × 2 grid', () => {
17
+ expect(rotated([[1, 2], [3, 4]])).toEqual([[3, 1], [4, 2]])
18
+ })
19
+
20
+ it('rotates a 3 × 3 grid', () => {
21
+ expect(rotated([[10, 11, 12], [13, 14, 15], [16, 17, 18]])).toEqual([
22
+ [16, 13, 10],
23
+ [17, 14, 11],
24
+ [18, 15, 12],
25
+ ])
26
+ })
27
+
28
+ it('leaves a 1 × 1 grid as it is', () => {
29
+ expect(rotated([[7]])).toEqual([[7]])
30
+ })
31
+
32
+ it('rotates a 4 × 4 grid with negatives and repeats', () => {
33
+ expect(
34
+ rotated([
35
+ [-1, 0, 0, 5],
36
+ [2, -3, 4, 0],
37
+ [0, 6, -7, 8],
38
+ [9, 0, 1, -2],
39
+ ]),
40
+ ).toEqual([
41
+ [9, 0, 2, -1],
42
+ [0, 6, -3, 0],
43
+ [1, -7, 4, 0],
44
+ [-2, 8, 0, 5],
45
+ ])
46
+ })
47
+
48
+ it('puts every cell in the right place for sizes 1 to 9', () => {
49
+ for (let n = 1; n <= 9; n++) {
50
+ const original = grid(n)
51
+ const result = rotated(grid(n))
52
+ for (let r = 0; r < n; r++) {
53
+ for (let c = 0; c < n; c++) {
54
+ // The new row r is the old column r, read from the bottom up.
55
+ expect(result[r]![c], `n = ${n}, cell [${r}][${c}]`).toBe(original[n - 1 - c]![r])
56
+ }
57
+ }
58
+ }
59
+ })
60
+
61
+ it('returns to the start after four turns', () => {
62
+ const matrix = grid(6)
63
+ for (let turn = 0; turn < 4; turn++) rotate(matrix)
64
+ expect(matrix).toEqual(grid(6))
65
+ })
66
+
67
+ it('changes the matrix it was given rather than returning a new one', () => {
68
+ const matrix = [[1, 2], [3, 4]]
69
+ rotate(matrix)
70
+ expect(matrix).toEqual([[3, 1], [4, 2]])
71
+ })
72
+ })
@@ -0,0 +1,40 @@
1
+ # 242 · Spiral Matrix
2
+
3
+ **Level 2 · Medium** · Pattern: matrix (shrinking boundaries) · Target: 25 min · NeetCode 150 · LeetCode 54
4
+
5
+ ## Problem
6
+
7
+ `matrix` is a grid with `m` rows and `n` columns. Read every value once, starting at the top-left corner and going clockwise round the outside: right along the top row, down the right-hand column, left along the bottom row, up the left-hand column. Then do the same for the next ring inside, and so on until every cell has been read.
8
+
9
+ Return the values in the order you read them. Don't modify `matrix`.
10
+
11
+ ## Examples
12
+
13
+ | `matrix` | Output |
14
+ |---|---|
15
+ | `[[2, 4, 6, 8], [10, 12, 14, 16], [18, 20, 22, 24]]` | `[2, 4, 6, 8, 16, 24, 22, 20, 18, 10, 12, 14]` |
16
+ | `[[5, 1, 9], [3, 0, 2], [8, 4, 6]]` | `[5, 1, 9, 2, 6, 4, 8, 3, 0]` |
17
+ | `[[3, -1, 4]]` | `[3, -1, 4]` |
18
+
19
+ ## Constraints
20
+
21
+ - `1 <= m, n <= 10`, and every row has the same length. The tests also use grids up to 200 cells on a side.
22
+ - `-100 <= matrix[r][c] <= 100` in the examples; the larger tests use bigger, all-different values.
23
+
24
+ ## Run it
25
+
26
+ ```bash
27
+ pnpm watch 242
28
+ ```
29
+
30
+ <details><summary>Target complexity (open after your first attempt)</summary>
31
+
32
+ Time O(m·n) · Space O(1) besides the output. Keeping a second grid that marks visited cells also runs in O(m·n) time but uses O(m·n) extra space. The tests don't time this one: every correct approach reads each cell once. The hard part is the last ring when it's a single row or column.
33
+
34
+ </details>
35
+
36
+ ## Follow-ups an interviewer might ask
37
+
38
+ - Do it the other way round: fill an `n × n` grid with `1, 2, …, n²` in spiral order (LeetCode 59).
39
+ - Read the spiral counter-clockwise, or starting from the centre outwards.
40
+ - How do you make sure a middle row or column isn't read twice when the inner ring is only one cell thick?