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,85 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { ladderLength } from './solution'
4
+
5
+ /** Every word of `length` letters drawn from `letters`, in a scrambled but fixed order. */
6
+ function allWords(letters: string, length: number): string[] {
7
+ let words = ['']
8
+ for (let i = 0; i < length; i++) words = words.flatMap((w) => [...letters].map((ch) => w + ch))
9
+ return words.map((w, i) => ({ w, key: (i * 7_919) % words.length })).sort((a, b) => a.key - b.key).map(({ w }) => w)
10
+ }
11
+
12
+ describe('313 word ladder', () => {
13
+ it('runs the README examples', () => {
14
+ expect(ladderLength('cold', 'warm', ['cord', 'card', 'ward', 'warm', 'worm', 'word'])).toBe(5)
15
+ expect(ladderLength('lamp', 'lime', ['limp', 'lame', 'lamb'])).toBe(0)
16
+ expect(ladderLength('ab', 'cb', ['cb'])).toBe(2)
17
+ })
18
+
19
+ it('returns 0 when the end word is in the list but cut off', () => {
20
+ expect(ladderLength('cat', 'dog', ['cot', 'cut', 'dig', 'dog'])).toBe(0)
21
+ })
22
+
23
+ it('returns 0 for an empty word list', () => {
24
+ expect(ladderLength('a', 'b', [])).toBe(0)
25
+ })
26
+
27
+ it('handles one-letter words', () => {
28
+ expect(ladderLength('a', 'c', ['a', 'b', 'c'])).toBe(2)
29
+ })
30
+
31
+ it('does not need the begin word in the list, but allows it', () => {
32
+ expect(ladderLength('hit', 'hot', ['hot'])).toBe(2)
33
+ expect(ladderLength('hit', 'hot', ['hit', 'hot'])).toBe(2)
34
+ })
35
+
36
+ it('finds the shortest chain when a longer one comes first in the list', () => {
37
+ const wordList = ['bbbb', 'abbb', 'aabb', 'aaab', 'baaa', 'bbaa', 'bbba', 'aaba', 'abba', 'aaaa', 'bbab']
38
+ // aaaa → aaab → aabb → abbb → bbbb is 5 words; the list holds detours too.
39
+ expect(ladderLength('aaaa', 'bbbb', wordList)).toBe(5)
40
+ })
41
+
42
+ it('changes letters only in place', () => {
43
+ // "tab" → "bat" would need reordering, which isn't a step.
44
+ expect(ladderLength('tab', 'bat', ['bat'])).toBe(0)
45
+ expect(ladderLength('tab', 'bat', ['bab', 'bat'])).toBe(3)
46
+ })
47
+
48
+ it('needs every intermediate word to be in the list', () => {
49
+ expect(ladderLength('lead', 'gold', ['load', 'goad', 'gold'])).toBe(4)
50
+ expect(ladderLength('lead', 'gold', ['load', 'gold'])).toBe(0)
51
+ })
52
+
53
+ it('does not modify the word list', () => {
54
+ const wordList = ['cord', 'card', 'ward', 'warm', 'worm', 'word']
55
+ ladderLength('cold', 'warm', wordList)
56
+ expect(wordList).toEqual(['cord', 'card', 'ward', 'warm', 'worm', 'word'])
57
+ })
58
+
59
+ it('finds a 21-word chain through a long corridor of words', () => {
60
+ // Each step changes one letter; no shortcuts exist, so the answer is the whole corridor.
61
+ const corridor: string[] = []
62
+ let word = 'aaaaaaaaaa'
63
+ for (let i = 0; i < 10; i++) {
64
+ word = `${word.slice(0, i)}b${word.slice(i + 1)}`
65
+ corridor.push(word)
66
+ }
67
+ for (let i = 0; i < 10; i++) {
68
+ word = `${word.slice(0, i)}c${word.slice(i + 1)}`
69
+ corridor.push(word)
70
+ }
71
+ expect(ladderLength('aaaaaaaaaa', 'cccccccccc', [...corridor].reverse())).toBe(21)
72
+ })
73
+ })
74
+
75
+ describe('313 performance', () => {
76
+ it('searches 15,630 six-letter words in under 750 ms (comparing each word with every other, O(n²·L), is too slow here)', () => {
77
+ // Every 6-letter word over a–e is present, so nearly all of them get explored. The end word sits at
78
+ // the far end of a corridor of words containing 'z': eeeeee → zeeeee → zzeeee → … → zzzzzz.
79
+ // aaaaaa needs 5 steps to reach aeeeee, 1 more to zeeeee, then 5 more: 12 words in all.
80
+ const corridor = [1, 2, 3, 4, 5, 6].map((zs) => 'z'.repeat(zs) + 'e'.repeat(6 - zs))
81
+ const wordList = [...allWords('abcde', 6), ...corridor]
82
+ const ms = timeMs(() => expect(ladderLength('aaaaaa', 'zzzzzz', wordList)).toBe(12))
83
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(750)
84
+ })
85
+ })
@@ -0,0 +1,43 @@
1
+ # 314 · Alien Dictionary
2
+
3
+ **Level 3 · Hard** · Pattern: graphs (topological sort) · Target: 40 min · NeetCode 150 · LeetCode 269
4
+
5
+ ## Problem
6
+
7
+ A language uses some of the lowercase letters `a`–`z`, but in an alphabet order you don't know. You're given `words`, a list of words from that language that is claimed to be sorted by that unknown order, the way a dictionary is sorted: compare two words letter by letter, and the first position where they differ decides which comes first. If one word is a prefix of the other, the shorter one comes first.
8
+
9
+ Return a string containing **every letter that appears in `words`**, each exactly once, in an order under which `words` really is sorted. If several orders work, return any of them; the tests check that your order is valid rather than comparing with one fixed answer. If no order can make the list sorted, return `""`.
10
+
11
+ Don't modify `words`.
12
+
13
+ ## Examples
14
+
15
+ | `words` | Output |
16
+ |---|---|
17
+ | `["qz", "qx", "zx", "zm", "xm"]` | `"qzxm"` (the only valid order) |
18
+ | `["ba", "ab", "bc"]` | `""` (b must come before a, and a before b) |
19
+ | `["mole", "mo"]` | `""` (a longer word can't come before its own prefix) |
20
+ | `["k"]` | `"k"` |
21
+
22
+ ## Constraints
23
+
24
+ - `1 <= words.length <= 100` on LeetCode, `1 <= words[i].length <= 100`, lowercase letters only.
25
+ - The performance test uses 20,000 words over all 26 letters.
26
+
27
+ ## Run it
28
+
29
+ ```bash
30
+ pnpm watch 314
31
+ ```
32
+
33
+ <details><summary>Target complexity (open after your first attempt)</summary>
34
+
35
+ Time O(C), where `C` is the total number of letters in `words` · Space O(U + E), with `U ≤ 26` distinct letters and `E ≤ 26²` ordering rules. Only neighbouring words need to be compared. Comparing every pair of words (O(n² · L)) is too slow for the performance test.
36
+
37
+ </details>
38
+
39
+ ## Follow-ups an interviewer might ask
40
+
41
+ - Why is it enough to compare each word only with the next one?
42
+ - How does your approach detect that the rules contradict each other? Would a depth-first version detect it differently?
43
+ - How would you tell whether the order is **unique**?
@@ -0,0 +1,127 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { alienOrder } from './solution'
4
+
5
+ /**
6
+ * Returns a description of why `order` is not a valid answer for `words`, or null if it is valid:
7
+ * it must hold every letter of `words` exactly once and nothing else, and `words` must be sorted under it.
8
+ */
9
+ function orderProblem(words: readonly string[], order: string): string | null {
10
+ const letters = new Set(words.join(''))
11
+ if (order.length !== new Set(order).size) return `"${order}" repeats a letter`
12
+ if (order.length !== letters.size || [...order].some((ch) => !letters.has(ch))) {
13
+ return `"${order}" must contain exactly the letters ${[...letters].sort().join('')}`
14
+ }
15
+ const rank = new Map([...order].map((ch, i) => [ch, i]))
16
+ for (let i = 0; i + 1 < words.length; i++) {
17
+ const a = words[i]!
18
+ const b = words[i + 1]!
19
+ const j = [...a].findIndex((ch, k) => ch !== b[k])
20
+ const inOrder = j === -1 ? a.length <= b.length : j < b.length && rank.get(a[j]!)! < rank.get(b[j]!)!
21
+ if (!inOrder) return `"${a}" comes before "${b}", which "${order}" does not allow`
22
+ }
23
+ return null
24
+ }
25
+
26
+ const expectValid = (words: readonly string[]): void => {
27
+ const order = alienOrder(words)
28
+ expect(orderProblem(words, order), `answer "${order}"`).toBeNull()
29
+ }
30
+
31
+ /** Deterministic pseudo-random generator (MINSTD). */
32
+ function makeRandom(seed: number): (limit: number) => number {
33
+ let state = seed
34
+ return (limit) => {
35
+ state = (state * 48_271) % 2_147_483_647
36
+ return state % limit
37
+ }
38
+ }
39
+
40
+ /** `count` random words over a scrambled 26-letter alphabet, sorted by that alphabet. */
41
+ function sortedAlienWords(count: number, seed: number): string[] {
42
+ const random = makeRandom(seed)
43
+ const alphabet = [...'abcdefghijklmnopqrstuvwxyz']
44
+ for (let i = alphabet.length - 1; i > 0; i--) {
45
+ const j = random(i + 1)
46
+ ;[alphabet[i], alphabet[j]] = [alphabet[j]!, alphabet[i]!]
47
+ }
48
+ const rank = new Map(alphabet.map((ch, i) => [ch, i]))
49
+ const words = Array.from({ length: count }, () => {
50
+ const length = 3 + random(8)
51
+ return Array.from({ length }, () => alphabet[random(26)]!).join('')
52
+ })
53
+ return words.sort((a, b) => {
54
+ for (let k = 0; k < Math.min(a.length, b.length); k++) {
55
+ if (a[k] !== b[k]) return rank.get(a[k]!)! - rank.get(b[k]!)!
56
+ }
57
+ return a.length - b.length
58
+ })
59
+ }
60
+
61
+ describe('314 alien dictionary', () => {
62
+ it('finds the only valid order in the first README example', () => {
63
+ expect(alienOrder(['qz', 'qx', 'zx', 'zm', 'xm'])).toBe('qzxm')
64
+ })
65
+
66
+ it('returns "" when the rules go round in a circle', () => {
67
+ expect(alienOrder(['ba', 'ab', 'bc'])).toBe('')
68
+ expect(alienOrder(['x', 'y', 'z', 'x'])).toBe('')
69
+ })
70
+
71
+ it('returns "" when a word comes before its own prefix', () => {
72
+ expect(alienOrder(['mole', 'mo'])).toBe('')
73
+ expect(alienOrder(['ab', 'cd', 'cde', 'cd'])).toBe('')
74
+ })
75
+
76
+ it('handles a single word, with or without repeated letters', () => {
77
+ expect(alienOrder(['k'])).toBe('k')
78
+ expectValid(['noon'])
79
+ })
80
+
81
+ it('includes letters that no rule mentions', () => {
82
+ // Only "w before t" is known; e, r and f can go anywhere.
83
+ expectValid(['wrf', 'tef'])
84
+ expect(alienOrder(['wrf', 'tef'])).toHaveLength(5)
85
+ })
86
+
87
+ it('accepts any valid order when several work', () => {
88
+ expectValid(['ca', 'cb'])
89
+ expectValid(['abc', 'abd', 'e'])
90
+ })
91
+
92
+ it('learns nothing after the first differing letter', () => {
93
+ // "dz" before "ea" says d before e, but nothing about z and a.
94
+ expectValid(['dz', 'ea', 'ez'])
95
+ })
96
+
97
+ it('allows a prefix before the longer word, and identical neighbours', () => {
98
+ expectValid(['ab', 'abc', 'abc', 'b'])
99
+ })
100
+
101
+ it('chains many rules into one long order', () => {
102
+ expect(alienOrder(['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'])).toBe('hgfedcba')
103
+ expectValid(['tq', 'tu', 'qp', 'ua', 'up'])
104
+ })
105
+
106
+ it('finds a full 26-letter order from a sorted list of random words', () => {
107
+ expectValid(sortedAlienWords(500, 7))
108
+ })
109
+
110
+ it('does not modify the input', () => {
111
+ const words = ['qz', 'qx', 'zx', 'zm', 'xm']
112
+ alienOrder(words)
113
+ expect(words).toEqual(['qz', 'qx', 'zx', 'zm', 'xm'])
114
+ })
115
+ })
116
+
117
+ describe('314 performance', () => {
118
+ it('orders 20,000 words in under 200 ms (comparing every pair of words, O(n²·L), is too slow here)', () => {
119
+ const words = sortedAlienWords(20_000, 42)
120
+ let order = ''
121
+ const ms = timeMs(() => {
122
+ order = alienOrder(words)
123
+ })
124
+ expect(orderProblem(words, order), `answer "${order}"`).toBeNull()
125
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
126
+ })
127
+ })
@@ -0,0 +1,42 @@
1
+ # 315 · Longest Increasing Path in a Matrix
2
+
3
+ **Level 3 · Hard** · Pattern: 2-D DP (memoized DFS) · Target: 40 min · NeetCode 150 · LeetCode 329
4
+
5
+ ## Problem
6
+
7
+ `matrix` is a grid of integers with `m` rows and `n` columns. A path moves from a cell to the cell directly above, below, left or right of it (never diagonally, never off the grid), and every step must go to a **strictly larger** value.
8
+
9
+ Return the number of cells in the longest such path. A single cell on its own counts as a path of length 1, so the answer is at least 1. Don't modify `matrix`.
10
+
11
+ ## Examples
12
+
13
+ | `matrix` | Output |
14
+ |---|---|
15
+ | `[[7, 8, 1], [6, 2, 0], [5, 4, 3]]` | `7` (for example `0 → 3 → 4 → 5 → 6 → 7 → 8`) |
16
+ | `[[4, 4], [4, 4]]` | `1` (equal neighbours don't count as increasing) |
17
+ | `[[-5]]` | `1` |
18
+
19
+ In the first example, no path can use the `1` and still be long: its only neighbours are `8` and `0`.
20
+
21
+ ## Constraints
22
+
23
+ - `1 <= m, n <= 200`, `-2^31 <= matrix[i][j] <= 2^31 - 1`.
24
+ - One test is a 39 × 40 grid with a winding path of 820 cells. The performance test uses a 14 × 14 grid where the number of increasing paths is huge, plus a 300 × 300 grid.
25
+
26
+ ## Run it
27
+
28
+ ```bash
29
+ pnpm watch 315
30
+ ```
31
+
32
+ <details><summary>Target complexity (open after your first attempt)</summary>
33
+
34
+ Time O(m · n) · Space O(m · n) for the stored results (plus up to O(m · n) recursion in the worst case). Exploring every increasing path from every cell without remembering results is exponential and fails the performance test.
35
+
36
+ </details>
37
+
38
+ ## Follow-ups an interviewer might ask
39
+
40
+ - Do you need a "visited" set during the search? Why can't a strictly increasing path loop back on itself?
41
+ - Solve it without recursion: process cells in order of value, or peel off cells with no smaller neighbour level by level (topological order).
42
+ - Return the path itself, not just its length.
@@ -0,0 +1,118 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { longestIncreasingPath } from './solution'
4
+
5
+ /**
6
+ * A grid of -1s with one winding path of increasing values 0, 1, 2, …: along row 0 left to right, down
7
+ * through a gap at the end of row 1, along row 2 right to left, down through a gap at the start of row 3,
8
+ * and so on. The rows of -1s keep the path from having shortcuts. Returns the grid and the path length.
9
+ */
10
+ function windingPath(pathRows: number, cols: number): { grid: number[][]; length: number } {
11
+ const grid = Array.from({ length: 2 * pathRows - 1 }, () => new Array<number>(cols).fill(-1))
12
+ let next = 0
13
+ for (let p = 0; p < pathRows; p++) {
14
+ const row = 2 * p
15
+ const leftToRight = p % 2 === 0
16
+ for (let k = 0; k < cols; k++) grid[row]![leftToRight ? k : cols - 1 - k] = next++
17
+ if (p + 1 < pathRows) grid[row + 1]![leftToRight ? cols - 1 : 0] = next++
18
+ }
19
+ return { grid, length: next }
20
+ }
21
+
22
+ /** grid[i][j] = i + j: every monotone staircase path is increasing, so there are very many of them. */
23
+ const staircase = (rows: number, cols: number): number[][] =>
24
+ Array.from({ length: rows }, (_, i) => Array.from({ length: cols }, (_, j) => i + j))
25
+
26
+ describe('315 longest increasing path in a matrix', () => {
27
+ it('runs the README examples', () => {
28
+ expect(
29
+ longestIncreasingPath([
30
+ [7, 8, 1],
31
+ [6, 2, 0],
32
+ [5, 4, 3],
33
+ ]),
34
+ ).toBe(7)
35
+ expect(
36
+ longestIncreasingPath([
37
+ [4, 4],
38
+ [4, 4],
39
+ ]),
40
+ ).toBe(1)
41
+ expect(longestIncreasingPath([[-5]])).toBe(1)
42
+ })
43
+
44
+ it('handles a single row and a single column', () => {
45
+ expect(longestIncreasingPath([[3, 1, 2, 5, 9, 0]])).toBe(4)
46
+ expect(longestIncreasingPath([[3], [1], [2], [5], [9], [0]])).toBe(4)
47
+ })
48
+
49
+ it('can go up and left as well as down and right', () => {
50
+ // 1 → 2 → 3 → 5 → 6 → 7 → 8 → 9
51
+ expect(
52
+ longestIncreasingPath([
53
+ [9, 8, 7],
54
+ [1, 1, 6],
55
+ [2, 3, 5],
56
+ ]),
57
+ ).toBe(8)
58
+ })
59
+
60
+ it('does not move diagonally', () => {
61
+ expect(
62
+ longestIncreasingPath([
63
+ [1, 0],
64
+ [0, 2],
65
+ ]),
66
+ ).toBe(2)
67
+ })
68
+
69
+ it('needs strictly larger values, not equal ones', () => {
70
+ expect(longestIncreasingPath([[1, 2, 2, 3]])).toBe(2)
71
+ })
72
+
73
+ it('handles negative and extreme values', () => {
74
+ expect(
75
+ longestIncreasingPath([
76
+ [-2_147_483_648, -1],
77
+ [2_147_483_647, 0],
78
+ ]),
79
+ ).toBe(4)
80
+ })
81
+
82
+ it('handles a staircase grid', () => {
83
+ expect(longestIncreasingPath(staircase(4, 6))).toBe(9)
84
+ })
85
+
86
+ it('follows a winding path of 819 cells (plus the -1 next to its start)', () => {
87
+ const { grid, length } = windingPath(20, 40)
88
+ expect(length).toBe(819)
89
+ expect(longestIncreasingPath(grid)).toBe(820)
90
+ })
91
+
92
+ it('does not modify the matrix', () => {
93
+ const matrix = [
94
+ [7, 8, 1],
95
+ [6, 2, 0],
96
+ [5, 4, 3],
97
+ ]
98
+ longestIncreasingPath(matrix)
99
+ expect(matrix).toEqual([
100
+ [7, 8, 1],
101
+ [6, 2, 0],
102
+ [5, 4, 3],
103
+ ])
104
+ })
105
+ })
106
+
107
+ describe('315 performance', () => {
108
+ it('solves a 14 × 14 staircase and a 300 × 300 grid in under 200 ms (trying every path without storing results is exponential)', () => {
109
+ const small = staircase(14, 14)
110
+ // Values 0, 1, 2 repeating along diagonals: the longest path is 3 cells.
111
+ const large = Array.from({ length: 300 }, (_, i) => Array.from({ length: 300 }, (_, j) => (i + j) % 3))
112
+ const ms = timeMs(() => {
113
+ expect(longestIncreasingPath(small)).toBe(27)
114
+ expect(longestIncreasingPath(large)).toBe(3)
115
+ })
116
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
117
+ })
118
+ })
@@ -0,0 +1,44 @@
1
+ # 316 · Swim in Rising Water
2
+
3
+ **Level 3 · Hard** · Pattern: graphs (Dijkstra / minimax path) · Target: 40 min · NeetCode 150 · LeetCode 778
4
+
5
+ ## Problem
6
+
7
+ `grid` is an `n` × `n` map of ground heights. Every value from `0` to `n² - 1` appears exactly once. Rain starts at time `0`, and at time `t` the water everywhere stands at height `t`.
8
+
9
+ You start in the top-left cell and want to reach the bottom-right cell. At time `t` you may move, as often as you like and instantly, between cells that are directly above, below, left or right of each other, as long as **both** cells have a height of at most `t`. You can't move diagonally or leave the grid.
10
+
11
+ Return the smallest time `t` at which you can get from the top-left cell to the bottom-right cell. Don't modify `grid`.
12
+
13
+ ## Examples
14
+
15
+ | `grid` | Output |
16
+ |---|---|
17
+ | `[[0, 3], [1, 2]]` | `2` (go down to 1, then right to 2) |
18
+ | `[[0]]` | `0` |
19
+ | `[[2, 0, 5], [7, 1, 4], [6, 8, 3]]` | `4` (`2 → 0 → 1 → 4 → 3`; the start itself is under water only from time 2) |
20
+
21
+ ## Constraints
22
+
23
+ - `1 <= n <= 50` on LeetCode, and `grid` is a permutation of `0 .. n² - 1`.
24
+ - The performance test uses a 150 × 150 grid (22,500 cells) built so the answer is close to `n²`.
25
+
26
+ ## Run it
27
+
28
+ ```bash
29
+ pnpm watch 316
30
+ ```
31
+
32
+ <details><summary>Target complexity (open after your first attempt)</summary>
33
+
34
+ Time O(n² log n) · Space O(n²). A heap-based search that always expands the lowest reachable cell, binary search on `t` with a flood fill per guess, or adding cells in order of height with union-find all reach this. Trying `t = 0, 1, 2, …` with a fresh flood fill each time is O(n⁴) and fails the performance test.
35
+
36
+ TypeScript has no built-in priority queue, so if you use one, write a small binary heap yourself.
37
+
38
+ </details>
39
+
40
+ ## Follow-ups an interviewer might ask
41
+
42
+ - Compare the heap approach with binary search plus flood fill. When would you pick each?
43
+ - How would union-find solve it, adding cells in order of height?
44
+ - What changes if heights can repeat, or the grid isn't square?
@@ -0,0 +1,138 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { timeMs } from '../../lib/perf'
3
+ import { swimInWater } from './solution'
4
+
5
+ /** Deterministic shuffle of 0..n-1 (Fisher-Yates with a seeded MINSTD generator). */
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
+ /**
18
+ * An n × n grid split by a wall down the middle column. Every cell off the wall gets a scrambled value
19
+ * below n² - n; the wall gets the top n values, with the lowest of them, n² - n, at one gap. Any route
20
+ * must cross the wall, and by time n² - n everything else is under water, so the answer is n² - n.
21
+ */
22
+ function walledGrid(n: number, gapRow: number, seed: number): number[][] {
23
+ const wallCol = Math.floor(n / 2)
24
+ const low = shuffledRange(n * n - n, seed)
25
+ const highs = shuffledRange(n - 1, seed + 1).map((v) => n * n - n + 1 + v)
26
+ let nextLow = 0
27
+ let nextHigh = 0
28
+ return Array.from({ length: n }, (_, r) =>
29
+ Array.from({ length: n }, (_, c) => {
30
+ if (c !== wallCol) return low[nextLow++]!
31
+ return r === gapRow ? n * n - n : highs[nextHigh++]!
32
+ }),
33
+ )
34
+ }
35
+
36
+ describe('316 swim in rising water', () => {
37
+ it('runs the README examples', () => {
38
+ expect(
39
+ swimInWater([
40
+ [0, 3],
41
+ [1, 2],
42
+ ]),
43
+ ).toBe(2)
44
+ expect(swimInWater([[0]])).toBe(0)
45
+ expect(
46
+ swimInWater([
47
+ [2, 0, 5],
48
+ [7, 1, 4],
49
+ [6, 8, 3],
50
+ ]),
51
+ ).toBe(4)
52
+ })
53
+
54
+ it('can never finish before the start or the end is under water', () => {
55
+ expect(
56
+ swimInWater([
57
+ [3, 0],
58
+ [1, 2],
59
+ ]),
60
+ ).toBe(3)
61
+ expect(
62
+ swimInWater([
63
+ [0, 1],
64
+ [2, 3],
65
+ ]),
66
+ ).toBe(3)
67
+ })
68
+
69
+ it('goes round the edge when the middle is high', () => {
70
+ // Along the top row and down the right column, nothing is above 8.
71
+ expect(
72
+ swimInWater([
73
+ [0, 1, 2, 3, 4],
74
+ [24, 23, 22, 21, 5],
75
+ [12, 11, 10, 20, 6],
76
+ [13, 18, 9, 19, 7],
77
+ [14, 15, 16, 17, 8],
78
+ ]),
79
+ ).toBe(8)
80
+ })
81
+
82
+ it('needs to move left and up as well as right and down', () => {
83
+ // The only route that stays below 17 snakes through the low cells, passing 16 on the way.
84
+ expect(
85
+ swimInWater([
86
+ [0, 1, 2, 3, 4],
87
+ [17, 18, 19, 20, 5],
88
+ [10, 8, 16, 7, 6],
89
+ [11, 21, 22, 23, 24],
90
+ [12, 13, 14, 15, 9],
91
+ ]),
92
+ ).toBe(16)
93
+ expect(
94
+ swimInWater([
95
+ [0, 17, 10, 11, 12],
96
+ [1, 18, 9, 19, 13],
97
+ [2, 20, 16, 21, 14],
98
+ [3, 22, 7, 23, 15],
99
+ [4, 5, 6, 24, 8],
100
+ ]),
101
+ ).toBe(16)
102
+ })
103
+
104
+ it('does not move diagonally', () => {
105
+ expect(
106
+ swimInWater([
107
+ [0, 8, 7],
108
+ [6, 1, 5],
109
+ [4, 3, 2],
110
+ ]),
111
+ ).toBe(6)
112
+ })
113
+
114
+ it('finds the gap in a wall', () => {
115
+ for (const gapRow of [0, 3, 6]) expect(swimInWater(walledGrid(7, gapRow, 5 + gapRow))).toBe(42)
116
+ })
117
+
118
+ it('does not modify the grid', () => {
119
+ const grid = [
120
+ [0, 3],
121
+ [1, 2],
122
+ ]
123
+ swimInWater(grid)
124
+ expect(grid).toEqual([
125
+ [0, 3],
126
+ [1, 2],
127
+ ])
128
+ })
129
+ })
130
+
131
+ describe('316 performance', () => {
132
+ it('solves a 150 × 150 grid in under 200 ms (trying t = 0, 1, 2, … with a fresh search each time, O(n⁴), is too slow here)', () => {
133
+ const n = 150
134
+ const grid = walledGrid(n, 97, 42)
135
+ const ms = timeMs(() => expect(swimInWater(grid)).toBe(n * n - n))
136
+ expect(ms, `took ${ms.toFixed(0)} ms`).toBeLessThan(200)
137
+ })
138
+ })
@@ -0,0 +1,28 @@
1
+ # P01 · Lending tools between neighbours
2
+
3
+ Category: Product sense · Level: core · Time: 35 min
4
+
5
+ People in the same street or building often own tools they use twice a year, while their neighbours buy the same drill or ladder again. Design a product that helps neighbours lend and borrow things like this.
6
+
7
+ Before you answer, find out what the goal is (a business, a community project, part of an existing app), where it would launch first, and what the people asking for it already know about who would use it.
8
+
9
+ ## If you ask
10
+
11
+ - It is a new venture with seed funding; the goal for the first year is to prove people keep coming back, not to make money.
12
+ - It launches in one mid-size city, in neighbourhoods of houses and of flats.
13
+ - Phone first; a web page for browsing is fine.
14
+ - Nothing is known yet about who would use it: no research has been done.
15
+ - Out of scope: delivery by the company and insurance products.
16
+
17
+ ## What a strong answer covers
18
+
19
+ - Sets a goal for the product before generating ideas, and comes back to it when choosing.
20
+ - Names more than one group of people who would lend or borrow, and picks one to serve first with a reason.
21
+ - Lists several concrete problems that group has today, and ranks them.
22
+ - Offers ideas that differ from one another, then chooses with criteria said out loud.
23
+ - Ends on what to build first and how to tell whether people come back.
24
+
25
+ ## Follow-ups
26
+
27
+ - The first month shows lots of listings and almost no borrowing. What do you look at, and what changes?
28
+ - A large hardware store offers to partner. What would you want from the deal, and what would worry you?
@@ -0,0 +1,28 @@
1
+ # P02 · Improve a city bike-share app
2
+
3
+ Category: Product sense · Level: core · Time: 35 min
4
+
5
+ A city runs a bike-share scheme with docking stations across its centre, and an app people use to find a bike, unlock it and pay. Riders rate the app poorly and the city wants it improved. How would you improve it?
6
+
7
+ Before you answer, find out what the city wants the improvement to achieve, who rides today and who doesn't, and which complaints come up most.
8
+
9
+ ## If you ask
10
+
11
+ - The city wants more trips per bike per day; ridership has been flat for two years.
12
+ - Two-thirds of trips are commuters with a yearly pass; the rest are day passes, mostly visitors.
13
+ - The most common complaints: stations empty when you arrive, full when you want to return, and a slow unlock.
14
+ - A team of six engineers and a designer for the next two quarters.
15
+ - Out of scope: buying more bikes or building new stations.
16
+
17
+ ## What a strong answer covers
18
+
19
+ - Turns the city's goal into a product goal before looking at features.
20
+ - Separates riders into groups whose journeys and needs differ, and chooses whom to focus on.
21
+ - Walks through a ride from start to end to find where it breaks, rather than listing features.
22
+ - Weighs each idea against impact on the goal and the effort the team has, and picks.
23
+ - States how trips per bike would be measured and what must not get worse.
24
+
25
+ ## Follow-ups
26
+
27
+ - The city adds electric bikes at a higher price. What changes in your plan?
28
+ - Your top idea needs data the operator of the docks won't share. What now?