@wazir-dev/cli 1.0.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 (629) hide show
  1. package/AGENTS.md +111 -0
  2. package/CHANGELOG.md +14 -0
  3. package/CONTRIBUTING.md +101 -0
  4. package/LICENSE +21 -0
  5. package/README.md +314 -0
  6. package/assets/composition-engine.mmd +34 -0
  7. package/assets/demo-script.sh +17 -0
  8. package/assets/logo-dark.svg +14 -0
  9. package/assets/logo.svg +14 -0
  10. package/assets/pipeline.mmd +39 -0
  11. package/assets/record-demo.sh +51 -0
  12. package/docs/README.md +51 -0
  13. package/docs/adapters/context-mode.md +60 -0
  14. package/docs/concepts/architecture.md +87 -0
  15. package/docs/concepts/artifact-model.md +60 -0
  16. package/docs/concepts/composition-engine.md +36 -0
  17. package/docs/concepts/indexing-and-recall.md +160 -0
  18. package/docs/concepts/observability.md +41 -0
  19. package/docs/concepts/roles-and-workflows.md +59 -0
  20. package/docs/concepts/terminology-policy.md +27 -0
  21. package/docs/getting-started/01-installation.md +78 -0
  22. package/docs/getting-started/02-first-run.md +102 -0
  23. package/docs/getting-started/03-adding-to-project.md +15 -0
  24. package/docs/getting-started/04-host-setup.md +15 -0
  25. package/docs/guides/ci-integration.md +15 -0
  26. package/docs/guides/creating-skills.md +15 -0
  27. package/docs/guides/expertise-module-authoring.md +15 -0
  28. package/docs/guides/hook-development.md +15 -0
  29. package/docs/guides/memory-and-learnings.md +34 -0
  30. package/docs/guides/multi-host-export.md +15 -0
  31. package/docs/guides/troubleshooting.md +101 -0
  32. package/docs/guides/writing-custom-roles.md +15 -0
  33. package/docs/plans/2026-03-15-cli-pipeline-integration-design.md +592 -0
  34. package/docs/plans/2026-03-15-cli-pipeline-integration-plan.md +598 -0
  35. package/docs/plans/2026-03-15-docs-enforcement-plan.md +238 -0
  36. package/docs/readmes/INDEX.md +99 -0
  37. package/docs/readmes/features/expertise/README.md +171 -0
  38. package/docs/readmes/features/exports/README.md +222 -0
  39. package/docs/readmes/features/hooks/README.md +103 -0
  40. package/docs/readmes/features/hooks/loop-cap-guard.md +133 -0
  41. package/docs/readmes/features/hooks/post-tool-capture.md +121 -0
  42. package/docs/readmes/features/hooks/post-tool-lint.md +130 -0
  43. package/docs/readmes/features/hooks/pre-compact-summary.md +122 -0
  44. package/docs/readmes/features/hooks/pre-tool-capture-route.md +100 -0
  45. package/docs/readmes/features/hooks/protected-path-write-guard.md +128 -0
  46. package/docs/readmes/features/hooks/session-start.md +119 -0
  47. package/docs/readmes/features/hooks/stop-handoff-harvest.md +125 -0
  48. package/docs/readmes/features/roles/README.md +157 -0
  49. package/docs/readmes/features/roles/clarifier.md +152 -0
  50. package/docs/readmes/features/roles/content-author.md +190 -0
  51. package/docs/readmes/features/roles/designer.md +193 -0
  52. package/docs/readmes/features/roles/executor.md +184 -0
  53. package/docs/readmes/features/roles/learner.md +210 -0
  54. package/docs/readmes/features/roles/planner.md +182 -0
  55. package/docs/readmes/features/roles/researcher.md +164 -0
  56. package/docs/readmes/features/roles/reviewer.md +184 -0
  57. package/docs/readmes/features/roles/specifier.md +162 -0
  58. package/docs/readmes/features/roles/verifier.md +215 -0
  59. package/docs/readmes/features/schemas/README.md +178 -0
  60. package/docs/readmes/features/skills/README.md +63 -0
  61. package/docs/readmes/features/skills/brainstorming.md +96 -0
  62. package/docs/readmes/features/skills/debugging.md +148 -0
  63. package/docs/readmes/features/skills/design.md +120 -0
  64. package/docs/readmes/features/skills/prepare-next.md +109 -0
  65. package/docs/readmes/features/skills/run-audit.md +159 -0
  66. package/docs/readmes/features/skills/scan-project.md +109 -0
  67. package/docs/readmes/features/skills/self-audit.md +176 -0
  68. package/docs/readmes/features/skills/tdd.md +137 -0
  69. package/docs/readmes/features/skills/using-skills.md +92 -0
  70. package/docs/readmes/features/skills/verification.md +120 -0
  71. package/docs/readmes/features/skills/writing-plans.md +104 -0
  72. package/docs/readmes/features/tooling/README.md +320 -0
  73. package/docs/readmes/features/workflows/README.md +186 -0
  74. package/docs/readmes/features/workflows/author.md +181 -0
  75. package/docs/readmes/features/workflows/clarify.md +154 -0
  76. package/docs/readmes/features/workflows/design-review.md +171 -0
  77. package/docs/readmes/features/workflows/design.md +169 -0
  78. package/docs/readmes/features/workflows/discover.md +162 -0
  79. package/docs/readmes/features/workflows/execute.md +173 -0
  80. package/docs/readmes/features/workflows/learn.md +167 -0
  81. package/docs/readmes/features/workflows/plan-review.md +165 -0
  82. package/docs/readmes/features/workflows/plan.md +170 -0
  83. package/docs/readmes/features/workflows/prepare-next.md +167 -0
  84. package/docs/readmes/features/workflows/review.md +169 -0
  85. package/docs/readmes/features/workflows/run-audit.md +191 -0
  86. package/docs/readmes/features/workflows/spec-challenge.md +159 -0
  87. package/docs/readmes/features/workflows/specify.md +160 -0
  88. package/docs/readmes/features/workflows/verify.md +177 -0
  89. package/docs/readmes/packages/README.md +50 -0
  90. package/docs/readmes/packages/ajv.md +117 -0
  91. package/docs/readmes/packages/context-mode.md +118 -0
  92. package/docs/readmes/packages/gray-matter.md +116 -0
  93. package/docs/readmes/packages/node-test.md +137 -0
  94. package/docs/readmes/packages/yaml.md +112 -0
  95. package/docs/reference/configuration-reference.md +159 -0
  96. package/docs/reference/expertise-index.md +52 -0
  97. package/docs/reference/git-flow.md +43 -0
  98. package/docs/reference/hooks.md +87 -0
  99. package/docs/reference/host-exports.md +50 -0
  100. package/docs/reference/launch-checklist.md +172 -0
  101. package/docs/reference/marketplace-listings.md +76 -0
  102. package/docs/reference/release-process.md +34 -0
  103. package/docs/reference/roles-reference.md +77 -0
  104. package/docs/reference/skills.md +33 -0
  105. package/docs/reference/templates.md +29 -0
  106. package/docs/reference/tooling-cli.md +94 -0
  107. package/docs/truth-claims.yaml +222 -0
  108. package/expertise/PROGRESS.md +63 -0
  109. package/expertise/README.md +18 -0
  110. package/expertise/antipatterns/PROGRESS.md +56 -0
  111. package/expertise/antipatterns/backend/api-design-antipatterns.md +1271 -0
  112. package/expertise/antipatterns/backend/auth-antipatterns.md +1195 -0
  113. package/expertise/antipatterns/backend/caching-antipatterns.md +622 -0
  114. package/expertise/antipatterns/backend/database-antipatterns.md +1038 -0
  115. package/expertise/antipatterns/backend/index.md +24 -0
  116. package/expertise/antipatterns/backend/microservices-antipatterns.md +850 -0
  117. package/expertise/antipatterns/code/architecture-antipatterns.md +919 -0
  118. package/expertise/antipatterns/code/async-antipatterns.md +622 -0
  119. package/expertise/antipatterns/code/code-smells.md +1186 -0
  120. package/expertise/antipatterns/code/dependency-antipatterns.md +1209 -0
  121. package/expertise/antipatterns/code/error-handling-antipatterns.md +1360 -0
  122. package/expertise/antipatterns/code/index.md +27 -0
  123. package/expertise/antipatterns/code/naming-and-abstraction.md +1118 -0
  124. package/expertise/antipatterns/code/state-management-antipatterns.md +1076 -0
  125. package/expertise/antipatterns/code/testing-antipatterns.md +1053 -0
  126. package/expertise/antipatterns/design/accessibility-antipatterns.md +1136 -0
  127. package/expertise/antipatterns/design/dark-patterns.md +1121 -0
  128. package/expertise/antipatterns/design/index.md +22 -0
  129. package/expertise/antipatterns/design/ui-antipatterns.md +1202 -0
  130. package/expertise/antipatterns/design/ux-antipatterns.md +680 -0
  131. package/expertise/antipatterns/frontend/css-layout-antipatterns.md +691 -0
  132. package/expertise/antipatterns/frontend/flutter-antipatterns.md +1827 -0
  133. package/expertise/antipatterns/frontend/index.md +23 -0
  134. package/expertise/antipatterns/frontend/mobile-antipatterns.md +573 -0
  135. package/expertise/antipatterns/frontend/react-antipatterns.md +1128 -0
  136. package/expertise/antipatterns/frontend/spa-antipatterns.md +1235 -0
  137. package/expertise/antipatterns/index.md +31 -0
  138. package/expertise/antipatterns/performance/index.md +20 -0
  139. package/expertise/antipatterns/performance/performance-antipatterns.md +1013 -0
  140. package/expertise/antipatterns/performance/premature-optimization.md +623 -0
  141. package/expertise/antipatterns/performance/scaling-antipatterns.md +785 -0
  142. package/expertise/antipatterns/process/ai-coding-antipatterns.md +853 -0
  143. package/expertise/antipatterns/process/code-review-antipatterns.md +656 -0
  144. package/expertise/antipatterns/process/deployment-antipatterns.md +920 -0
  145. package/expertise/antipatterns/process/index.md +23 -0
  146. package/expertise/antipatterns/process/technical-debt-antipatterns.md +647 -0
  147. package/expertise/antipatterns/security/index.md +20 -0
  148. package/expertise/antipatterns/security/secrets-antipatterns.md +849 -0
  149. package/expertise/antipatterns/security/security-theater.md +843 -0
  150. package/expertise/antipatterns/security/vulnerability-patterns.md +801 -0
  151. package/expertise/architecture/PROGRESS.md +70 -0
  152. package/expertise/architecture/data/caching-architecture.md +671 -0
  153. package/expertise/architecture/data/data-consistency.md +574 -0
  154. package/expertise/architecture/data/data-modeling.md +536 -0
  155. package/expertise/architecture/data/event-streams-and-queues.md +634 -0
  156. package/expertise/architecture/data/index.md +25 -0
  157. package/expertise/architecture/data/search-architecture.md +663 -0
  158. package/expertise/architecture/data/sql-vs-nosql.md +708 -0
  159. package/expertise/architecture/decisions/architecture-decision-records.md +640 -0
  160. package/expertise/architecture/decisions/build-vs-buy.md +616 -0
  161. package/expertise/architecture/decisions/index.md +23 -0
  162. package/expertise/architecture/decisions/monolith-to-microservices.md +790 -0
  163. package/expertise/architecture/decisions/technology-selection.md +616 -0
  164. package/expertise/architecture/distributed/cap-theorem-and-tradeoffs.md +800 -0
  165. package/expertise/architecture/distributed/circuit-breaker-bulkhead.md +741 -0
  166. package/expertise/architecture/distributed/consensus-and-coordination.md +796 -0
  167. package/expertise/architecture/distributed/distributed-systems-fundamentals.md +564 -0
  168. package/expertise/architecture/distributed/idempotency-and-retry.md +796 -0
  169. package/expertise/architecture/distributed/index.md +25 -0
  170. package/expertise/architecture/distributed/saga-pattern.md +797 -0
  171. package/expertise/architecture/foundations/architectural-thinking.md +460 -0
  172. package/expertise/architecture/foundations/coupling-and-cohesion.md +770 -0
  173. package/expertise/architecture/foundations/design-principles-solid.md +649 -0
  174. package/expertise/architecture/foundations/domain-driven-design.md +719 -0
  175. package/expertise/architecture/foundations/index.md +25 -0
  176. package/expertise/architecture/foundations/separation-of-concerns.md +472 -0
  177. package/expertise/architecture/foundations/twelve-factor-app.md +797 -0
  178. package/expertise/architecture/index.md +34 -0
  179. package/expertise/architecture/integration/api-design-graphql.md +638 -0
  180. package/expertise/architecture/integration/api-design-grpc.md +804 -0
  181. package/expertise/architecture/integration/api-design-rest.md +892 -0
  182. package/expertise/architecture/integration/index.md +25 -0
  183. package/expertise/architecture/integration/third-party-integration.md +795 -0
  184. package/expertise/architecture/integration/webhooks-and-callbacks.md +1152 -0
  185. package/expertise/architecture/integration/websockets-realtime.md +791 -0
  186. package/expertise/architecture/mobile-architecture/index.md +22 -0
  187. package/expertise/architecture/mobile-architecture/mobile-app-architecture.md +780 -0
  188. package/expertise/architecture/mobile-architecture/mobile-backend-for-frontend.md +670 -0
  189. package/expertise/architecture/mobile-architecture/offline-first.md +719 -0
  190. package/expertise/architecture/mobile-architecture/push-and-sync.md +782 -0
  191. package/expertise/architecture/patterns/cqrs-event-sourcing.md +717 -0
  192. package/expertise/architecture/patterns/event-driven.md +797 -0
  193. package/expertise/architecture/patterns/hexagonal-clean-architecture.md +870 -0
  194. package/expertise/architecture/patterns/index.md +27 -0
  195. package/expertise/architecture/patterns/layered-architecture.md +736 -0
  196. package/expertise/architecture/patterns/microservices.md +753 -0
  197. package/expertise/architecture/patterns/modular-monolith.md +692 -0
  198. package/expertise/architecture/patterns/monolith.md +626 -0
  199. package/expertise/architecture/patterns/plugin-architecture.md +735 -0
  200. package/expertise/architecture/patterns/serverless.md +780 -0
  201. package/expertise/architecture/scaling/database-scaling.md +615 -0
  202. package/expertise/architecture/scaling/feature-flags-and-rollouts.md +757 -0
  203. package/expertise/architecture/scaling/horizontal-vs-vertical.md +606 -0
  204. package/expertise/architecture/scaling/index.md +24 -0
  205. package/expertise/architecture/scaling/multi-tenancy.md +800 -0
  206. package/expertise/architecture/scaling/stateless-design.md +787 -0
  207. package/expertise/backend/embedded-firmware.md +625 -0
  208. package/expertise/backend/go.md +853 -0
  209. package/expertise/backend/index.md +24 -0
  210. package/expertise/backend/java-spring.md +448 -0
  211. package/expertise/backend/node-typescript.md +625 -0
  212. package/expertise/backend/python-fastapi.md +724 -0
  213. package/expertise/backend/rust.md +458 -0
  214. package/expertise/backend/solidity.md +711 -0
  215. package/expertise/composition-map.yaml +443 -0
  216. package/expertise/content/foundations/content-modeling.md +395 -0
  217. package/expertise/content/foundations/editorial-standards.md +449 -0
  218. package/expertise/content/foundations/index.md +24 -0
  219. package/expertise/content/foundations/microcopy.md +455 -0
  220. package/expertise/content/foundations/terminology-governance.md +509 -0
  221. package/expertise/content/index.md +34 -0
  222. package/expertise/content/patterns/accessibility-copy.md +518 -0
  223. package/expertise/content/patterns/index.md +24 -0
  224. package/expertise/content/patterns/notification-content.md +433 -0
  225. package/expertise/content/patterns/sample-content.md +486 -0
  226. package/expertise/content/patterns/state-copy.md +439 -0
  227. package/expertise/design/PROGRESS.md +58 -0
  228. package/expertise/design/disciplines/dark-mode-theming.md +577 -0
  229. package/expertise/design/disciplines/design-systems.md +595 -0
  230. package/expertise/design/disciplines/index.md +25 -0
  231. package/expertise/design/disciplines/information-architecture.md +800 -0
  232. package/expertise/design/disciplines/interaction-design.md +788 -0
  233. package/expertise/design/disciplines/responsive-design.md +552 -0
  234. package/expertise/design/disciplines/usability-testing.md +516 -0
  235. package/expertise/design/disciplines/user-research.md +792 -0
  236. package/expertise/design/foundations/accessibility-design.md +796 -0
  237. package/expertise/design/foundations/color-theory.md +797 -0
  238. package/expertise/design/foundations/iconography.md +795 -0
  239. package/expertise/design/foundations/index.md +26 -0
  240. package/expertise/design/foundations/motion-and-animation.md +653 -0
  241. package/expertise/design/foundations/rtl-design.md +585 -0
  242. package/expertise/design/foundations/spacing-and-layout.md +607 -0
  243. package/expertise/design/foundations/typography.md +800 -0
  244. package/expertise/design/foundations/visual-hierarchy.md +761 -0
  245. package/expertise/design/index.md +32 -0
  246. package/expertise/design/patterns/authentication-flows.md +474 -0
  247. package/expertise/design/patterns/content-consumption.md +789 -0
  248. package/expertise/design/patterns/data-display.md +618 -0
  249. package/expertise/design/patterns/e-commerce.md +1494 -0
  250. package/expertise/design/patterns/feedback-and-states.md +642 -0
  251. package/expertise/design/patterns/forms-and-input.md +819 -0
  252. package/expertise/design/patterns/gamification.md +801 -0
  253. package/expertise/design/patterns/index.md +31 -0
  254. package/expertise/design/patterns/microinteractions.md +449 -0
  255. package/expertise/design/patterns/navigation.md +800 -0
  256. package/expertise/design/patterns/notifications.md +705 -0
  257. package/expertise/design/patterns/onboarding.md +700 -0
  258. package/expertise/design/patterns/search-and-filter.md +601 -0
  259. package/expertise/design/patterns/settings-and-preferences.md +768 -0
  260. package/expertise/design/patterns/social-and-community.md +748 -0
  261. package/expertise/design/platforms/desktop-native.md +612 -0
  262. package/expertise/design/platforms/index.md +25 -0
  263. package/expertise/design/platforms/mobile-android.md +825 -0
  264. package/expertise/design/platforms/mobile-cross-platform.md +983 -0
  265. package/expertise/design/platforms/mobile-ios.md +699 -0
  266. package/expertise/design/platforms/tablet.md +794 -0
  267. package/expertise/design/platforms/web-dashboard.md +790 -0
  268. package/expertise/design/platforms/web-responsive.md +550 -0
  269. package/expertise/design/psychology/behavioral-nudges.md +449 -0
  270. package/expertise/design/psychology/cognitive-load.md +1191 -0
  271. package/expertise/design/psychology/error-psychology.md +778 -0
  272. package/expertise/design/psychology/index.md +22 -0
  273. package/expertise/design/psychology/persuasive-design.md +736 -0
  274. package/expertise/design/psychology/user-mental-models.md +623 -0
  275. package/expertise/design/tooling/open-pencil.md +266 -0
  276. package/expertise/frontend/angular.md +1073 -0
  277. package/expertise/frontend/desktop-electron.md +546 -0
  278. package/expertise/frontend/flutter.md +782 -0
  279. package/expertise/frontend/index.md +27 -0
  280. package/expertise/frontend/native-android.md +409 -0
  281. package/expertise/frontend/native-ios.md +490 -0
  282. package/expertise/frontend/react-native.md +1160 -0
  283. package/expertise/frontend/react.md +808 -0
  284. package/expertise/frontend/vue.md +1089 -0
  285. package/expertise/humanize/domain-rules-code.md +79 -0
  286. package/expertise/humanize/domain-rules-content.md +67 -0
  287. package/expertise/humanize/domain-rules-technical-docs.md +56 -0
  288. package/expertise/humanize/index.md +35 -0
  289. package/expertise/humanize/self-audit-checklist.md +87 -0
  290. package/expertise/humanize/sentence-patterns.md +218 -0
  291. package/expertise/humanize/vocabulary-blacklist.md +105 -0
  292. package/expertise/i18n/PROGRESS.md +65 -0
  293. package/expertise/i18n/advanced/accessibility-and-i18n.md +28 -0
  294. package/expertise/i18n/advanced/bidirectional-text-algorithm.md +38 -0
  295. package/expertise/i18n/advanced/complex-scripts.md +30 -0
  296. package/expertise/i18n/advanced/performance-and-i18n.md +27 -0
  297. package/expertise/i18n/advanced/testing-i18n.md +28 -0
  298. package/expertise/i18n/content/content-adaptation.md +23 -0
  299. package/expertise/i18n/content/locale-specific-formatting.md +23 -0
  300. package/expertise/i18n/content/machine-translation-integration.md +28 -0
  301. package/expertise/i18n/content/translation-management.md +29 -0
  302. package/expertise/i18n/foundations/date-time-calendars.md +67 -0
  303. package/expertise/i18n/foundations/i18n-architecture.md +272 -0
  304. package/expertise/i18n/foundations/locale-and-language-tags.md +79 -0
  305. package/expertise/i18n/foundations/numbers-currency-units.md +61 -0
  306. package/expertise/i18n/foundations/pluralization-and-gender.md +109 -0
  307. package/expertise/i18n/foundations/string-externalization.md +236 -0
  308. package/expertise/i18n/foundations/text-direction-bidi.md +241 -0
  309. package/expertise/i18n/foundations/unicode-and-encoding.md +86 -0
  310. package/expertise/i18n/index.md +38 -0
  311. package/expertise/i18n/platform/backend-i18n.md +31 -0
  312. package/expertise/i18n/platform/flutter-i18n.md +148 -0
  313. package/expertise/i18n/platform/native-android-i18n.md +36 -0
  314. package/expertise/i18n/platform/native-ios-i18n.md +36 -0
  315. package/expertise/i18n/platform/react-i18n.md +103 -0
  316. package/expertise/i18n/platform/web-css-i18n.md +81 -0
  317. package/expertise/i18n/rtl/arabic-specific.md +175 -0
  318. package/expertise/i18n/rtl/hebrew-specific.md +149 -0
  319. package/expertise/i18n/rtl/rtl-animations-and-transitions.md +111 -0
  320. package/expertise/i18n/rtl/rtl-forms-and-input.md +161 -0
  321. package/expertise/i18n/rtl/rtl-fundamentals.md +211 -0
  322. package/expertise/i18n/rtl/rtl-icons-and-images.md +181 -0
  323. package/expertise/i18n/rtl/rtl-layout-mirroring.md +252 -0
  324. package/expertise/i18n/rtl/rtl-navigation-and-gestures.md +107 -0
  325. package/expertise/i18n/rtl/rtl-testing-and-qa.md +147 -0
  326. package/expertise/i18n/rtl/rtl-typography.md +160 -0
  327. package/expertise/index.md +113 -0
  328. package/expertise/index.yaml +216 -0
  329. package/expertise/infrastructure/cloud-aws.md +597 -0
  330. package/expertise/infrastructure/cloud-gcp.md +599 -0
  331. package/expertise/infrastructure/cybersecurity.md +816 -0
  332. package/expertise/infrastructure/database-mongodb.md +447 -0
  333. package/expertise/infrastructure/database-postgres.md +400 -0
  334. package/expertise/infrastructure/devops-cicd.md +787 -0
  335. package/expertise/infrastructure/index.md +27 -0
  336. package/expertise/performance/PROGRESS.md +50 -0
  337. package/expertise/performance/backend/api-latency.md +1204 -0
  338. package/expertise/performance/backend/background-jobs.md +506 -0
  339. package/expertise/performance/backend/connection-pooling.md +1209 -0
  340. package/expertise/performance/backend/database-query-optimization.md +515 -0
  341. package/expertise/performance/backend/index.md +23 -0
  342. package/expertise/performance/backend/rate-limiting-and-throttling.md +971 -0
  343. package/expertise/performance/foundations/algorithmic-complexity.md +954 -0
  344. package/expertise/performance/foundations/caching-strategies.md +489 -0
  345. package/expertise/performance/foundations/concurrency-and-parallelism.md +847 -0
  346. package/expertise/performance/foundations/index.md +24 -0
  347. package/expertise/performance/foundations/measuring-and-profiling.md +440 -0
  348. package/expertise/performance/foundations/memory-management.md +964 -0
  349. package/expertise/performance/foundations/performance-budgets.md +1314 -0
  350. package/expertise/performance/index.md +31 -0
  351. package/expertise/performance/infrastructure/auto-scaling.md +1059 -0
  352. package/expertise/performance/infrastructure/cdn-and-edge.md +1081 -0
  353. package/expertise/performance/infrastructure/index.md +22 -0
  354. package/expertise/performance/infrastructure/load-balancing.md +1081 -0
  355. package/expertise/performance/infrastructure/observability.md +1079 -0
  356. package/expertise/performance/mobile/index.md +23 -0
  357. package/expertise/performance/mobile/mobile-animations.md +544 -0
  358. package/expertise/performance/mobile/mobile-memory-battery.md +416 -0
  359. package/expertise/performance/mobile/mobile-network.md +452 -0
  360. package/expertise/performance/mobile/mobile-rendering.md +599 -0
  361. package/expertise/performance/mobile/mobile-startup-time.md +505 -0
  362. package/expertise/performance/platform-specific/flutter-performance.md +647 -0
  363. package/expertise/performance/platform-specific/index.md +22 -0
  364. package/expertise/performance/platform-specific/node-performance.md +1307 -0
  365. package/expertise/performance/platform-specific/postgres-performance.md +1366 -0
  366. package/expertise/performance/platform-specific/react-performance.md +1403 -0
  367. package/expertise/performance/web/bundle-optimization.md +1239 -0
  368. package/expertise/performance/web/image-and-media.md +636 -0
  369. package/expertise/performance/web/index.md +24 -0
  370. package/expertise/performance/web/network-optimization.md +1133 -0
  371. package/expertise/performance/web/rendering-performance.md +1098 -0
  372. package/expertise/performance/web/ssr-and-hydration.md +918 -0
  373. package/expertise/performance/web/web-vitals.md +1374 -0
  374. package/expertise/quality/accessibility.md +985 -0
  375. package/expertise/quality/evidence-based-verification.md +499 -0
  376. package/expertise/quality/index.md +24 -0
  377. package/expertise/quality/ml-model-audit.md +614 -0
  378. package/expertise/quality/performance.md +600 -0
  379. package/expertise/quality/testing-api.md +891 -0
  380. package/expertise/quality/testing-mobile.md +496 -0
  381. package/expertise/quality/testing-web.md +849 -0
  382. package/expertise/security/PROGRESS.md +54 -0
  383. package/expertise/security/agentic-identity.md +540 -0
  384. package/expertise/security/compliance-frameworks.md +601 -0
  385. package/expertise/security/data/data-encryption.md +364 -0
  386. package/expertise/security/data/data-privacy-gdpr.md +692 -0
  387. package/expertise/security/data/database-security.md +1171 -0
  388. package/expertise/security/data/index.md +22 -0
  389. package/expertise/security/data/pii-handling.md +531 -0
  390. package/expertise/security/foundations/authentication.md +1041 -0
  391. package/expertise/security/foundations/authorization.md +603 -0
  392. package/expertise/security/foundations/cryptography.md +1001 -0
  393. package/expertise/security/foundations/index.md +25 -0
  394. package/expertise/security/foundations/owasp-top-10.md +1354 -0
  395. package/expertise/security/foundations/secrets-management.md +1217 -0
  396. package/expertise/security/foundations/secure-sdlc.md +700 -0
  397. package/expertise/security/foundations/supply-chain-security.md +698 -0
  398. package/expertise/security/index.md +31 -0
  399. package/expertise/security/infrastructure/cloud-security-aws.md +1296 -0
  400. package/expertise/security/infrastructure/cloud-security-gcp.md +1376 -0
  401. package/expertise/security/infrastructure/container-security.md +721 -0
  402. package/expertise/security/infrastructure/incident-response.md +1295 -0
  403. package/expertise/security/infrastructure/index.md +24 -0
  404. package/expertise/security/infrastructure/logging-and-monitoring.md +1618 -0
  405. package/expertise/security/infrastructure/network-security.md +1337 -0
  406. package/expertise/security/mobile/index.md +23 -0
  407. package/expertise/security/mobile/mobile-android-security.md +1218 -0
  408. package/expertise/security/mobile/mobile-binary-protection.md +1229 -0
  409. package/expertise/security/mobile/mobile-data-storage.md +1265 -0
  410. package/expertise/security/mobile/mobile-ios-security.md +1401 -0
  411. package/expertise/security/mobile/mobile-network-security.md +1520 -0
  412. package/expertise/security/smart-contract-security.md +594 -0
  413. package/expertise/security/testing/index.md +22 -0
  414. package/expertise/security/testing/penetration-testing.md +1258 -0
  415. package/expertise/security/testing/security-code-review.md +1765 -0
  416. package/expertise/security/testing/threat-modeling.md +1074 -0
  417. package/expertise/security/testing/vulnerability-scanning.md +1062 -0
  418. package/expertise/security/web/api-security.md +586 -0
  419. package/expertise/security/web/cors-and-headers.md +433 -0
  420. package/expertise/security/web/csrf.md +562 -0
  421. package/expertise/security/web/file-upload.md +1477 -0
  422. package/expertise/security/web/index.md +25 -0
  423. package/expertise/security/web/injection.md +1375 -0
  424. package/expertise/security/web/session-management.md +1101 -0
  425. package/expertise/security/web/xss.md +1158 -0
  426. package/exports/README.md +17 -0
  427. package/exports/hosts/claude/.claude/agents/clarifier.md +42 -0
  428. package/exports/hosts/claude/.claude/agents/content-author.md +63 -0
  429. package/exports/hosts/claude/.claude/agents/designer.md +55 -0
  430. package/exports/hosts/claude/.claude/agents/executor.md +55 -0
  431. package/exports/hosts/claude/.claude/agents/learner.md +51 -0
  432. package/exports/hosts/claude/.claude/agents/planner.md +53 -0
  433. package/exports/hosts/claude/.claude/agents/researcher.md +43 -0
  434. package/exports/hosts/claude/.claude/agents/reviewer.md +54 -0
  435. package/exports/hosts/claude/.claude/agents/specifier.md +47 -0
  436. package/exports/hosts/claude/.claude/agents/verifier.md +71 -0
  437. package/exports/hosts/claude/.claude/commands/author.md +42 -0
  438. package/exports/hosts/claude/.claude/commands/clarify.md +38 -0
  439. package/exports/hosts/claude/.claude/commands/design-review.md +46 -0
  440. package/exports/hosts/claude/.claude/commands/design.md +44 -0
  441. package/exports/hosts/claude/.claude/commands/discover.md +37 -0
  442. package/exports/hosts/claude/.claude/commands/execute.md +48 -0
  443. package/exports/hosts/claude/.claude/commands/learn.md +38 -0
  444. package/exports/hosts/claude/.claude/commands/plan-review.md +42 -0
  445. package/exports/hosts/claude/.claude/commands/plan.md +39 -0
  446. package/exports/hosts/claude/.claude/commands/prepare-next.md +37 -0
  447. package/exports/hosts/claude/.claude/commands/review.md +40 -0
  448. package/exports/hosts/claude/.claude/commands/run-audit.md +41 -0
  449. package/exports/hosts/claude/.claude/commands/spec-challenge.md +41 -0
  450. package/exports/hosts/claude/.claude/commands/specify.md +38 -0
  451. package/exports/hosts/claude/.claude/commands/verify.md +37 -0
  452. package/exports/hosts/claude/.claude/settings.json +34 -0
  453. package/exports/hosts/claude/CLAUDE.md +19 -0
  454. package/exports/hosts/claude/export.manifest.json +38 -0
  455. package/exports/hosts/claude/host-package.json +67 -0
  456. package/exports/hosts/codex/AGENTS.md +19 -0
  457. package/exports/hosts/codex/export.manifest.json +38 -0
  458. package/exports/hosts/codex/host-package.json +41 -0
  459. package/exports/hosts/cursor/.cursor/hooks.json +16 -0
  460. package/exports/hosts/cursor/.cursor/rules/wazir-core.mdc +19 -0
  461. package/exports/hosts/cursor/export.manifest.json +38 -0
  462. package/exports/hosts/cursor/host-package.json +42 -0
  463. package/exports/hosts/gemini/GEMINI.md +19 -0
  464. package/exports/hosts/gemini/export.manifest.json +38 -0
  465. package/exports/hosts/gemini/host-package.json +41 -0
  466. package/hooks/README.md +18 -0
  467. package/hooks/definitions/loop_cap_guard.yaml +21 -0
  468. package/hooks/definitions/post_tool_capture.yaml +24 -0
  469. package/hooks/definitions/pre_compact_summary.yaml +19 -0
  470. package/hooks/definitions/pre_tool_capture_route.yaml +19 -0
  471. package/hooks/definitions/protected_path_write_guard.yaml +19 -0
  472. package/hooks/definitions/session_start.yaml +19 -0
  473. package/hooks/definitions/stop_handoff_harvest.yaml +20 -0
  474. package/hooks/loop-cap-guard +17 -0
  475. package/hooks/post-tool-lint +36 -0
  476. package/hooks/protected-path-write-guard +17 -0
  477. package/hooks/session-start +41 -0
  478. package/llms-full.txt +2355 -0
  479. package/llms.txt +43 -0
  480. package/package.json +79 -0
  481. package/roles/README.md +20 -0
  482. package/roles/clarifier.md +42 -0
  483. package/roles/content-author.md +63 -0
  484. package/roles/designer.md +55 -0
  485. package/roles/executor.md +55 -0
  486. package/roles/learner.md +51 -0
  487. package/roles/planner.md +53 -0
  488. package/roles/researcher.md +43 -0
  489. package/roles/reviewer.md +54 -0
  490. package/roles/specifier.md +47 -0
  491. package/roles/verifier.md +71 -0
  492. package/schemas/README.md +24 -0
  493. package/schemas/accepted-learning.schema.json +20 -0
  494. package/schemas/author-artifact.schema.json +156 -0
  495. package/schemas/clarification.schema.json +19 -0
  496. package/schemas/design-artifact.schema.json +80 -0
  497. package/schemas/docs-claim.schema.json +18 -0
  498. package/schemas/export-manifest.schema.json +20 -0
  499. package/schemas/hook.schema.json +67 -0
  500. package/schemas/host-export-package.schema.json +18 -0
  501. package/schemas/implementation-plan.schema.json +19 -0
  502. package/schemas/proposed-learning.schema.json +19 -0
  503. package/schemas/research.schema.json +18 -0
  504. package/schemas/review.schema.json +29 -0
  505. package/schemas/run-manifest.schema.json +18 -0
  506. package/schemas/spec-challenge.schema.json +18 -0
  507. package/schemas/spec.schema.json +20 -0
  508. package/schemas/usage.schema.json +102 -0
  509. package/schemas/verification-proof.schema.json +29 -0
  510. package/schemas/wazir-manifest.schema.json +173 -0
  511. package/skills/README.md +40 -0
  512. package/skills/brainstorming/SKILL.md +77 -0
  513. package/skills/debugging/SKILL.md +50 -0
  514. package/skills/design/SKILL.md +61 -0
  515. package/skills/dispatching-parallel-agents/SKILL.md +128 -0
  516. package/skills/executing-plans/SKILL.md +70 -0
  517. package/skills/finishing-a-development-branch/SKILL.md +169 -0
  518. package/skills/humanize/SKILL.md +123 -0
  519. package/skills/init-pipeline/SKILL.md +124 -0
  520. package/skills/prepare-next/SKILL.md +20 -0
  521. package/skills/receiving-code-review/SKILL.md +123 -0
  522. package/skills/requesting-code-review/SKILL.md +105 -0
  523. package/skills/requesting-code-review/code-reviewer.md +108 -0
  524. package/skills/run-audit/SKILL.md +197 -0
  525. package/skills/scan-project/SKILL.md +41 -0
  526. package/skills/self-audit/SKILL.md +153 -0
  527. package/skills/subagent-driven-development/SKILL.md +154 -0
  528. package/skills/subagent-driven-development/code-quality-reviewer-prompt.md +26 -0
  529. package/skills/subagent-driven-development/implementer-prompt.md +102 -0
  530. package/skills/subagent-driven-development/spec-reviewer-prompt.md +61 -0
  531. package/skills/tdd/SKILL.md +23 -0
  532. package/skills/using-git-worktrees/SKILL.md +163 -0
  533. package/skills/using-skills/SKILL.md +95 -0
  534. package/skills/verification/SKILL.md +22 -0
  535. package/skills/wazir/SKILL.md +463 -0
  536. package/skills/writing-plans/SKILL.md +30 -0
  537. package/skills/writing-skills/SKILL.md +157 -0
  538. package/skills/writing-skills/anthropic-best-practices.md +122 -0
  539. package/skills/writing-skills/persuasion-principles.md +50 -0
  540. package/templates/README.md +20 -0
  541. package/templates/artifacts/README.md +10 -0
  542. package/templates/artifacts/accepted-learning.md +19 -0
  543. package/templates/artifacts/accepted-learning.template.json +12 -0
  544. package/templates/artifacts/author.md +74 -0
  545. package/templates/artifacts/author.template.json +19 -0
  546. package/templates/artifacts/clarification.md +21 -0
  547. package/templates/artifacts/clarification.template.json +12 -0
  548. package/templates/artifacts/execute-notes.md +19 -0
  549. package/templates/artifacts/implementation-plan.md +21 -0
  550. package/templates/artifacts/implementation-plan.template.json +11 -0
  551. package/templates/artifacts/learning-proposal.md +19 -0
  552. package/templates/artifacts/next-run-handoff.md +21 -0
  553. package/templates/artifacts/plan-review.md +19 -0
  554. package/templates/artifacts/proposed-learning.template.json +12 -0
  555. package/templates/artifacts/research.md +21 -0
  556. package/templates/artifacts/research.template.json +12 -0
  557. package/templates/artifacts/review-findings.md +19 -0
  558. package/templates/artifacts/review.template.json +11 -0
  559. package/templates/artifacts/run-manifest.template.json +8 -0
  560. package/templates/artifacts/spec-challenge.md +19 -0
  561. package/templates/artifacts/spec-challenge.template.json +11 -0
  562. package/templates/artifacts/spec.md +21 -0
  563. package/templates/artifacts/spec.template.json +12 -0
  564. package/templates/artifacts/verification-proof.md +19 -0
  565. package/templates/artifacts/verification-proof.template.json +11 -0
  566. package/templates/examples/accepted-learning.example.json +14 -0
  567. package/templates/examples/author.example.json +152 -0
  568. package/templates/examples/clarification.example.json +15 -0
  569. package/templates/examples/docs-claim.example.json +8 -0
  570. package/templates/examples/export-manifest.example.json +7 -0
  571. package/templates/examples/host-export-package.example.json +11 -0
  572. package/templates/examples/implementation-plan.example.json +17 -0
  573. package/templates/examples/proposed-learning.example.json +13 -0
  574. package/templates/examples/research.example.json +15 -0
  575. package/templates/examples/research.example.md +6 -0
  576. package/templates/examples/review.example.json +17 -0
  577. package/templates/examples/run-manifest.example.json +9 -0
  578. package/templates/examples/spec-challenge.example.json +14 -0
  579. package/templates/examples/spec.example.json +21 -0
  580. package/templates/examples/verification-proof.example.json +21 -0
  581. package/templates/examples/wazir-manifest.example.yaml +65 -0
  582. package/templates/task-definition-schema.md +99 -0
  583. package/tooling/README.md +20 -0
  584. package/tooling/src/adapters/context-mode.js +50 -0
  585. package/tooling/src/capture/command.js +376 -0
  586. package/tooling/src/capture/store.js +99 -0
  587. package/tooling/src/capture/usage.js +270 -0
  588. package/tooling/src/checks/branches.js +50 -0
  589. package/tooling/src/checks/brand-truth.js +110 -0
  590. package/tooling/src/checks/changelog.js +231 -0
  591. package/tooling/src/checks/command-registry.js +36 -0
  592. package/tooling/src/checks/commits.js +102 -0
  593. package/tooling/src/checks/docs-drift.js +103 -0
  594. package/tooling/src/checks/docs-truth.js +201 -0
  595. package/tooling/src/checks/runtime-surface.js +156 -0
  596. package/tooling/src/cli.js +116 -0
  597. package/tooling/src/command-options.js +56 -0
  598. package/tooling/src/commands/validate.js +320 -0
  599. package/tooling/src/doctor/command.js +91 -0
  600. package/tooling/src/export/command.js +77 -0
  601. package/tooling/src/export/compiler.js +498 -0
  602. package/tooling/src/guards/loop-cap-guard.js +52 -0
  603. package/tooling/src/guards/protected-path-write-guard.js +67 -0
  604. package/tooling/src/index/command.js +152 -0
  605. package/tooling/src/index/storage.js +1061 -0
  606. package/tooling/src/index/summarizers.js +261 -0
  607. package/tooling/src/loaders.js +18 -0
  608. package/tooling/src/project-root.js +22 -0
  609. package/tooling/src/recall/command.js +225 -0
  610. package/tooling/src/schema-validator.js +30 -0
  611. package/tooling/src/state-root.js +40 -0
  612. package/tooling/src/status/command.js +71 -0
  613. package/wazir.manifest.yaml +135 -0
  614. package/workflows/README.md +19 -0
  615. package/workflows/author.md +42 -0
  616. package/workflows/clarify.md +38 -0
  617. package/workflows/design-review.md +46 -0
  618. package/workflows/design.md +44 -0
  619. package/workflows/discover.md +37 -0
  620. package/workflows/execute.md +48 -0
  621. package/workflows/learn.md +38 -0
  622. package/workflows/plan-review.md +42 -0
  623. package/workflows/plan.md +39 -0
  624. package/workflows/prepare-next.md +37 -0
  625. package/workflows/review.md +40 -0
  626. package/workflows/run-audit.md +41 -0
  627. package/workflows/spec-challenge.md +41 -0
  628. package/workflows/specify.md +38 -0
  629. package/workflows/verify.md +37 -0
@@ -0,0 +1,954 @@
1
+ # Algorithmic Complexity: A Performance Engineering Reference
2
+
3
+ ## Purpose
4
+
5
+ This module provides a practitioner-oriented guide to algorithmic complexity,
6
+ grounded in real-world timing data, production bug reports, and measured
7
+ before/after improvements. Every claim includes concrete numbers. The goal is
8
+ to help engineers make correct data-structure and algorithm choices the first
9
+ time, and to recognize accidental complexity in existing code.
10
+
11
+ ---
12
+
13
+ ## 1. Big O in Practice: What the Numbers Actually Mean
14
+
15
+ Big O describes how an algorithm's cost grows as input size (n) grows. The
16
+ constant factors, cache behavior, and memory allocation patterns that Big O
17
+ ignores are real, but at sufficient scale the growth rate always dominates.
18
+
19
+ ### 1.1 Concrete Timing Table
20
+
21
+ The following table uses a baseline of 1 microsecond per unit operation to
22
+ illustrate how growth rates diverge. Real-world absolute times vary by
23
+ language and hardware, but the *ratios* between complexity classes hold.
24
+
25
+ | n | O(1) | O(log n) | O(n) | O(n log n) | O(n^2) | O(2^n) |
26
+ |----------|--------|----------|---------|------------|------------|--------------|
27
+ | 10 | 1 us | 3.3 us | 10 us | 33 us | 100 us | 1,024 us |
28
+ | 100 | 1 us | 6.6 us | 100 us | 660 us | 10 ms | heat death |
29
+ | 1,000 | 1 us | 10 us | 1 ms | 10 ms | 1 s | heat death |
30
+ | 10,000 | 1 us | 13 us | 10 ms | 130 ms | 100 s | heat death |
31
+ | 100,000 | 1 us | 17 us | 100 ms | 1.7 s | 2.8 hours | heat death |
32
+ | 1,000,000| 1 us | 20 us | 1 s | 20 s | 11.6 days | heat death |
33
+
34
+ Source: growth-rate mathematics; 1 us baseline per unit op.
35
+
36
+ ### 1.2 The "Doubling Test"
37
+
38
+ A quick way to identify complexity in running code:
39
+ - Double n; if time roughly doubles -> O(n)
40
+ - Double n; if time roughly quadruples -> O(n^2)
41
+ - Double n; if time barely changes -> O(log n) or O(1)
42
+ - Double n; if time increases by a constant addend -> O(log n)
43
+
44
+ This technique, recommended by Sedgewick and Wayne in "Algorithms, 4th Ed.",
45
+ is invaluable for profiling black-box systems where source is unavailable.
46
+
47
+ ### 1.3 Why O(n^2) Seems Fine Until It Isn't
48
+
49
+ At n = 100, an O(n^2) algorithm does 10,000 operations -- often under 1 ms on
50
+ modern hardware. Engineers ship it, the code works, and then a customer loads
51
+ 10,000 items. Now the algorithm does 100,000,000 operations, taking 100
52
+ seconds. At n = 100,000, it takes 2.8 hours. This is the "accidental
53
+ quadratic cliff" that has caused outages at companies from Slack to the Linux
54
+ kernel (see Section 5).
55
+
56
+ ---
57
+
58
+ ## 2. Data Structure Selection Guide
59
+
60
+ ### 2.1 Core Operations Comparison
61
+
62
+ All times are average-case unless noted. "Worst" column shows degenerate-case
63
+ complexity (e.g., hash collisions, unbalanced trees).
64
+
65
+ | Data Structure | Insert | Lookup | Delete | Ordered? | Worst Insert | Worst Lookup |
66
+ |----------------------|--------|--------|--------|----------|--------------|--------------|
67
+ | Hash Map / Hash Set | O(1) | O(1) | O(1) | No | O(n) | O(n) |
68
+ | Balanced BST (RB/AVL)| O(log n)| O(log n)| O(log n)| Yes | O(log n) | O(log n) |
69
+ | Sorted Array | O(n) | O(log n)| O(n) | Yes | O(n) | O(log n) |
70
+ | Unsorted Array | O(1)* | O(n) | O(n) | No | O(1)* | O(n) |
71
+ | Linked List | O(1)** | O(n) | O(n) | No | O(1)** | O(n) |
72
+ | Skip List | O(log n)| O(log n)| O(log n)| Yes | O(n) | O(n) |
73
+ | Trie | O(k) | O(k) | O(k) | Yes*** | O(k) | O(k) |
74
+ | Bloom Filter | O(k) | O(k) | N/A | No | O(k) | O(k) |
75
+ | B-Tree | O(log n)| O(log n)| O(log n)| Yes | O(log n) | O(log n) |
76
+
77
+ \* Amortized O(1) append; O(n) for arbitrary-position insert.
78
+ \** O(1) only if you have a pointer to the insertion point.
79
+ \*** Tries provide lexicographic ordering.
80
+ k = key length (for Trie) or number of hash functions (for Bloom Filter).
81
+
82
+ ### 2.2 Measured Benchmarks: Java Collections
83
+
84
+ Benchmark data from Baeldung (2024) for Java HashMap vs TreeMap, measured in
85
+ microseconds per operation on a dataset of 100,000 entries:
86
+
87
+ | Operation | HashMap | TreeMap | Ratio |
88
+ |-------------|---------|---------|-------|
89
+ | put() | 0.019 us| 0.087 us| 4.6x |
90
+ | get() | 0.011 us| 0.069 us| 6.3x |
91
+ | containsKey | 0.009 us| 0.063 us| 7.0x |
92
+ | remove() | 0.010 us| 0.071 us| 7.1x |
93
+ | iteration | fastest with LinkedHashMap (2-3x faster than HashMap) |
94
+
95
+ Source: Baeldung, "Java TreeMap vs HashMap" (https://www.baeldung.com/java-treemap-vs-hashmap)
96
+
97
+ ### 2.3 Measured Benchmarks: JavaScript Set.has() vs Array.includes()
98
+
99
+ Benchmarks from multiple sources (DEV Community, MeasureThat.net, 2024) show:
100
+
101
+ | Array Size | Array.includes() | Set.has() | Speedup |
102
+ |------------|-------------------|-----------|---------|
103
+ | 10 | 12 ns | 18 ns | 0.7x (Array wins) |
104
+ | 100 | 85 ns | 18 ns | 4.7x |
105
+ | 1,000 | 790 ns | 18 ns | 44x |
106
+ | 10,000 | 7,800 ns | 18 ns | 433x |
107
+ | 100,000 | 78,000 ns | 18 ns | 4,333x |
108
+ | 1,000,000 | 780,000 ns | 18 ns | 43,333x |
109
+
110
+ Key insight: Array.includes() is O(n) -- it scans linearly. Set.has() is
111
+ O(1) -- it uses a hash table internally. For arrays under ~10 elements,
112
+ Array.includes() wins due to lower overhead and better cache locality. Above
113
+ that threshold, Set.has() dominates and the gap widens linearly with n.
114
+
115
+ Source: DEV Community, "Why Set.has() is Faster Than Array.includes()"
116
+ (https://dev.to/programminginblood/why-sethas-is-faster-than-arrayincludes-for-finding-items-4hh)
117
+
118
+ ### 2.4 When to Use What: Decision Tree
119
+
120
+ ```
121
+ Need key-value pairs?
122
+ |
123
+ +-- Yes: Need sorted/ordered keys?
124
+ | |
125
+ | +-- Yes: Need range queries or nearest-key? --> TreeMap / BTreeMap
126
+ | |
127
+ | +-- No: Need insertion-order iteration? --> LinkedHashMap
128
+ | |
129
+ | +-- No: General purpose --> HashMap / dict / Map
130
+ |
131
+ +-- No: Need membership testing only?
132
+ |
133
+ +-- Yes: Can tolerate false positives (~1%)?
134
+ | |
135
+ | +-- Yes: Memory constrained? --> Bloom Filter
136
+ | |
137
+ | +-- No: --> HashSet / Set
138
+ |
139
+ +-- No: Need ordered traversal?
140
+ |
141
+ +-- Yes: Mostly reads, rarely writes? --> Sorted Array + binary search
142
+ |
143
+ +-- Yes: Frequent inserts/deletes? --> Balanced BST / Skip List
144
+ |
145
+ +-- No: Need fast append + index access? --> Dynamic Array / Vec
146
+ |
147
+ +-- No: Need fast insert/delete at arbitrary position? --> Linked List
148
+ |
149
+ +-- No: String prefix matching? --> Trie
150
+ ```
151
+
152
+ ---
153
+
154
+ ## 3. Common "Accidental Quadratic" Bugs
155
+
156
+ Nelson Elhage's "Accidentally Quadratic" blog (accidentallyquadratic.tumblr.com)
157
+ documents dozens of real-world cases. The core pattern: calling an O(n)
158
+ operation inside an O(n) loop yields O(n^2) total work. This section catalogs
159
+ the most common variants.
160
+
161
+ ### 3.1 Nested Lookup in a Loop
162
+
163
+ ```python
164
+ # BAD: O(n * m) where m = len(blocklist)
165
+ def filter_users(users, blocklist):
166
+ return [u for u in users if u.id not in blocklist] # list scan each time
167
+
168
+ # GOOD: O(n + m) -- build set once, check in O(1)
169
+ def filter_users(users, blocklist):
170
+ blocked_ids = set(blocklist) # O(m) one-time cost
171
+ return [u for u in users if u.id not in blocked_ids] # O(1) per check
172
+ ```
173
+
174
+ At n = 10,000 users and m = 5,000 blocklist entries:
175
+ - BAD: ~50,000,000 comparisons -> ~500 ms
176
+ - GOOD: ~15,000 hash lookups + 5,000 set-build -> ~1 ms
177
+ - Speedup: 500x
178
+
179
+ ### 3.2 Array.includes() Inside a Filter Loop (JavaScript)
180
+
181
+ ```javascript
182
+ // BAD: O(n * m)
183
+ const result = data.filter(item => whitelist.includes(item.id));
184
+
185
+ // GOOD: O(n + m)
186
+ const whiteSet = new Set(whitelist.map(w => w));
187
+ const result = data.filter(item => whiteSet.has(item.id));
188
+ ```
189
+
190
+ With 50,000 data items and 10,000 whitelist entries:
191
+ - BAD: 500,000,000 comparisons -> ~5 seconds
192
+ - GOOD: ~60,000 operations -> ~0.6 ms
193
+ - Speedup: ~8,300x
194
+
195
+ ### 3.3 Repeated String Concatenation
196
+
197
+ Strings are immutable in Python, JavaScript, Java, Go, and most other
198
+ languages. Concatenating in a loop copies the entire accumulated string each
199
+ time, yielding O(n^2) total character copies.
200
+
201
+ ```python
202
+ # BAD: O(n^2) -- copies grow as 1 + 2 + 3 + ... + n = n(n+1)/2
203
+ result = ""
204
+ for line in lines:
205
+ result += line + "\n"
206
+
207
+ # GOOD: O(n) -- join allocates once
208
+ result = "\n".join(lines)
209
+ ```
210
+
211
+ PyPy's documentation (2023) confirms this is quadratic even with their JIT
212
+ compiler. Benchmark on 100,000 lines of 80 characters each:
213
+ - += loop: ~12 seconds (quadratic growth confirmed by doubling test)
214
+ - "\n".join(): ~8 ms
215
+ - Speedup: ~1,500x
216
+
217
+ Source: PyPy blog, "Repeated string concatenation is quadratic in PyPy (and CPython)"
218
+ (https://pypy.org/posts/2023/01/string-concatenation-quadratic.html)
219
+
220
+ ### 3.4 Naive Duplicate Detection
221
+
222
+ ```javascript
223
+ // BAD: O(n^2) -- indexOf is O(n), called n times
224
+ function hasDuplicates(arr) {
225
+ for (let i = 0; i < arr.length; i++) {
226
+ if (arr.indexOf(arr[i]) !== i) return true;
227
+ }
228
+ return false;
229
+ }
230
+
231
+ // GOOD: O(n) -- Set tracks seen values
232
+ function hasDuplicates(arr) {
233
+ return new Set(arr).size !== arr.length;
234
+ }
235
+ ```
236
+
237
+ ### 3.5 DOM Manipulation in Loops
238
+
239
+ ```javascript
240
+ // BAD: O(n) reflows -- each append triggers layout recalculation
241
+ for (const item of items) {
242
+ document.getElementById('list').innerHTML += `<li>${item}</li>`;
243
+ }
244
+
245
+ // GOOD: O(1) reflow -- batch update
246
+ const fragment = document.createDocumentFragment();
247
+ for (const item of items) {
248
+ const li = document.createElement('li');
249
+ li.textContent = item;
250
+ fragment.appendChild(li);
251
+ }
252
+ document.getElementById('list').appendChild(fragment);
253
+ ```
254
+
255
+ With 5,000 items: BAD takes ~4 seconds (each innerHTML+= re-parses the
256
+ entire accumulated HTML). GOOD takes ~15 ms.
257
+
258
+ ### 3.6 Real-World Production Incidents
259
+
260
+ **Linux Kernel -- /proc/$pid/maps enumeration (Elhage, 2015):**
261
+ Enumerating /proc/$pid/maps was O(n^2) in the number of memory-mapped regions.
262
+ Databases using thousands of connections (each a thread with its own maps)
263
+ experienced multi-second stalls during monitoring tool scrapes.
264
+
265
+ **Ruby Parser -- non-ASCII input (Elhage, 2014):**
266
+ A Ruby parser performed quadratic work on non-ASCII input due to repeated
267
+ re-encoding. A 1,500-line file took 2.5 seconds to parse. Fix: encode to
268
+ UTF-32 once upfront, reducing parse time to 900 ms (2.8x speedup), and
269
+ eliminating the quadratic growth curve entirely.
270
+
271
+ **Slack -- message rendering (2019):**
272
+ Slack's message rendering experienced quadratic slowdowns in channels with
273
+ many custom emoji reactions, as each reaction triggered a linear scan of
274
+ existing reactions. Channels with 50+ unique reactions saw multi-second
275
+ render times.
276
+
277
+ Source: accidentallyquadratic.tumblr.com; Julia Evans, "Quadratic algorithms are slow
278
+ (and hashmaps are fast)" (https://jvns.ca/blog/2021/09/10/hashmaps-make-things-fast/)
279
+
280
+ ---
281
+
282
+ ## 4. Sorting Algorithm Selection
283
+
284
+ ### 4.1 Comparison of Sorting Algorithms
285
+
286
+ | Algorithm | Best | Average | Worst | Space | Stable? | In-place? |
287
+ |---------------|----------|----------|----------|-------|---------|-----------|
288
+ | Insertion Sort| O(n) | O(n^2) | O(n^2) | O(1) | Yes | Yes |
289
+ | Merge Sort | O(n lg n)| O(n lg n)| O(n lg n)| O(n) | Yes | No |
290
+ | Quick Sort | O(n lg n)| O(n lg n)| O(n^2) | O(lg n)| No | Yes |
291
+ | Heap Sort | O(n lg n)| O(n lg n)| O(n lg n)| O(1) | No | Yes |
292
+ | Tim Sort | O(n) | O(n lg n)| O(n lg n)| O(n) | Yes | No |
293
+ | Radix Sort | O(nk) | O(nk) | O(nk) | O(n+k)| Yes | No |
294
+ | Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(k) | Yes | No |
295
+
296
+ n = number of elements; k = key size (radix) or range (counting); lg = log base 2.
297
+
298
+ ### 4.2 When to Use Each
299
+
300
+ **Insertion Sort (n < ~50):**
301
+ Best for very small arrays. Most standard library implementations (including
302
+ Timsort and introsort) fall back to insertion sort below a threshold of 16-64
303
+ elements. At n = 20, insertion sort typically beats quicksort due to lower
304
+ constant overhead: ~0.4 us vs ~0.8 us (OpenDSA empirical benchmarks).
305
+
306
+ **Timsort (general purpose, partially sorted data):**
307
+ Default in Python (since 2002) and Java (since JDK 7). Exploits existing
308
+ runs of sorted data (common in real-world datasets). On already-sorted data:
309
+ O(n) -- a single pass confirms order. On random data: competitive with
310
+ optimized quicksort, typically within 10-20% on benchmarks.
311
+
312
+ **Quicksort (general purpose, random data):**
313
+ Fastest in practice for random data due to excellent cache locality and low
314
+ constant factors. The "median of three" pivot selection virtually eliminates
315
+ the O(n^2) worst case. Introsort (used in C++ std::sort) switches to heapsort
316
+ after O(log n) recursion depth, guaranteeing O(n log n) worst case.
317
+
318
+ **Merge Sort (stability required, linked lists, external sort):**
319
+ Guaranteed O(n log n) worst case. Natural choice for linked lists (no random
320
+ access needed, O(1) space for linked list merge). Standard for external
321
+ sorting (disk-based) because it accesses data sequentially.
322
+
323
+ **Radix Sort (fixed-length integer keys):**
324
+ O(nk) where k is the number of digits. For 32-bit integers, k = 4 (sorting
325
+ by byte). At n > 100,000 with integer keys, radix sort typically outperforms
326
+ comparison sorts by 2-5x. However, the OpenDSA empirical benchmarks show
327
+ radix sort as "a surprisingly poor performer" for short arrays due to
328
+ setup overhead.
329
+
330
+ **Counting Sort (small key range):**
331
+ When the range of values k is O(n) or smaller, counting sort achieves O(n)
332
+ time. Ideal for sorting characters (k = 256), ages (k = 150), or status
333
+ codes (k ~ 50).
334
+
335
+ Source: OpenDSA, "An Empirical Comparison of Sorting Algorithms"
336
+ (https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/SortingEmpirical.html)
337
+
338
+ ### 4.3 Sorting Selection Decision Tree
339
+
340
+ ```
341
+ Is n < 50?
342
+ |
343
+ +-- Yes --> Insertion Sort
344
+ |
345
+ +-- No: Are keys integers with known small range?
346
+ |
347
+ +-- Yes: Is range k <= n? --> Counting Sort
348
+ |
349
+ +-- Yes: Are keys fixed-width? --> Radix Sort
350
+ |
351
+ +-- No: Is stability required?
352
+ |
353
+ +-- Yes: Is data partially sorted? --> Timsort
354
+ |
355
+ +-- Yes: General --> Merge Sort
356
+ |
357
+ +-- No: Need guaranteed O(n lg n) worst case? --> Heap Sort / Introsort
358
+ |
359
+ +-- No: General purpose --> Quicksort (median-of-3)
360
+ ```
361
+
362
+ ---
363
+
364
+ ## 5. Space-Time Tradeoffs
365
+
366
+ ### 5.1 Memoization / Caching
367
+
368
+ Trading memory for computation by storing previously computed results.
369
+
370
+ **Fibonacci -- the canonical example:**
371
+
372
+ | Approach | Time | Space | fib(40) wall time |
373
+ |-----------------|----------|-------|-------------------|
374
+ | Naive recursive | O(2^n) | O(n) | ~1.2 seconds |
375
+ | Memoized | O(n) | O(n) | ~0.001 ms |
376
+ | Bottom-up DP | O(n) | O(1) | ~0.001 ms |
377
+
378
+ The naive approach computes fib(40) by making ~2^40 = 1.1 trillion function
379
+ calls. Memoization reduces this to exactly 40 calls.
380
+
381
+ Source: GeeksforGeeks, "Complete Guide On Complexity Analysis"
382
+ (https://www.geeksforgeeks.org/dsa/complete-guide-on-complexity-analysis/)
383
+
384
+ **Real-world example -- API response caching:**
385
+ A service computing user recommendations with an O(n * m) algorithm (n users,
386
+ m items). Without caching: 500 ms per request. With a TTL cache (1-minute
387
+ expiry): 0.5 ms cache hit, amortized over 1000 requests per minute yields
388
+ 99.9% cache hit rate and average response time of ~1 ms.
389
+
390
+ ### 5.2 Lookup Tables / Precomputation
391
+
392
+ Pre-computing results for all possible inputs when the input space is bounded.
393
+
394
+ | Technique | Space Cost | Time Savings | Use Case |
395
+ |-------------------|------------|--------------|--------------------------------|
396
+ | Sin/cos table | 2.9 KB (360 floats) | O(1) vs O(~20) CORDIC ops | Game engines, embedded |
397
+ | CRC32 table | 1 KB (256 uint32) | ~10x speedup over bit-by-bit | Network checksums |
398
+ | Character class | 256 bytes | O(1) vs branching chain | Parsing, lexing |
399
+ | RGB->palette | 16 MB (256^3) | O(1) vs nearest-neighbor search | Image processing |
400
+
401
+ ### 5.3 Bloom Filters
402
+
403
+ A probabilistic data structure that answers "is X in the set?" with either
404
+ "definitely no" or "probably yes" (with a tunable false positive rate).
405
+
406
+ **Space efficiency benchmarks (AWS ElastiCache, 2024):**
407
+
408
+ | Elements | HashSet Memory | Bloom Filter (1% FP) | Savings |
409
+ |----------|---------------|----------------------|---------|
410
+ | 100,000 | 6.4 MB | 120 KB | 98% |
411
+ | 1,000,000| 64 MB | 1.2 MB | 98% |
412
+ | 10,000,000| 640 MB | 12 MB | 98% |
413
+
414
+ False positive rate tuning:
415
+ - 10 bits per element, 7 hash functions -> 0.82% FP rate
416
+ - 8 bits per element (1 byte), 5-6 hash functions -> ~2% FP rate
417
+ - Adding 4.8 bits per element reduces FP rate by 10x
418
+
419
+ Reducing false positives from 1% to 1 in 5,000,000 still saves 93% memory
420
+ compared to a full HashSet.
421
+
422
+ **Production use cases:**
423
+ - Google Bigtable: Bloom filters on SSTables avoid disk reads for non-existent
424
+ rows. A read for a missing key costs ~10ms without a Bloom filter (disk
425
+ seek) vs ~0.01ms with one (in-memory bit check).
426
+ - Medium: URL deduplication in their recommendation engine uses Bloom filters
427
+ to track billions of seen URLs in ~300 MB instead of ~30 GB.
428
+ - Bitcoin SPV nodes: Use Bloom filters to request only relevant transactions,
429
+ reducing bandwidth by ~99%.
430
+
431
+ Source: Wikipedia, "Bloom filter"; AWS, "Implement fast, space-efficient lookups
432
+ using Bloom filters in Amazon ElastiCache"
433
+ (https://aws.amazon.com/blogs/database/implement-fast-space-efficient-lookups-using-bloom-filters-in-amazon-elasticache/)
434
+
435
+ ### 5.4 Trading Space for Time -- Summary Matrix
436
+
437
+ | Technique | Space Cost | Time Reduction | Best When |
438
+ |------------------|------------|----------------------|-------------------------------|
439
+ | Memoization | O(n) | Exponential -> linear | Overlapping subproblems |
440
+ | Hash index | O(n) | O(n) -> O(1) lookup | Repeated lookups |
441
+ | Lookup table | O(input space)| Eliminates computation| Small, bounded input space |
442
+ | Bloom filter | ~10 bits/elem| Avoids expensive ops | Membership test, memory tight |
443
+ | Precomputed sort | O(n) | O(n) -> O(log n) search| Read-heavy, write-rare |
444
+ | Materialized view | O(result set)| Avoids re-aggregation| Dashboards, repeated queries |
445
+
446
+ ---
447
+
448
+ ## 6. Amortized Complexity
449
+
450
+ Amortized analysis examines the average cost per operation over a worst-case
451
+ sequence of operations, not just the worst case of a single operation.
452
+
453
+ ### 6.1 Dynamic Arrays (ArrayList, Vec, std::vector)
454
+
455
+ When a dynamic array runs out of capacity, it allocates a new buffer (typically
456
+ 2x the current size) and copies all existing elements.
457
+
458
+ **Single append**: Worst case O(n) when resizing occurs.
459
+ **Amortized append**: O(1) per operation.
460
+
461
+ **Why doubling works -- the mathematical argument:**
462
+ After n insertions, the total number of copies is:
463
+ n + n/2 + n/4 + n/8 + ... = 2n (geometric series)
464
+
465
+ Total work for n insertions = n (actual inserts) + 2n (copies) = 3n = O(n).
466
+ Per insertion: O(1) amortized.
467
+
468
+ **If you grew by a constant (e.g., +10 each time):**
469
+ Total copies = 10 + 20 + 30 + ... + n = O(n^2). This is why Python lists,
470
+ Java ArrayList, Rust Vec, and C++ vector all use multiplicative growth (1.5x
471
+ or 2x), never additive growth.
472
+
473
+ Source: Cornell CS3110, "Amortized analysis and dynamic tables"
474
+ (https://www.cs.cornell.edu/courses/cs3110/2009sp/lectures/lec21.html)
475
+
476
+ ### 6.2 Hash Table Resizing
477
+
478
+ Hash tables resize (rehash) when the load factor exceeds a threshold (commonly
479
+ 0.75). Resizing copies all n elements to a new, larger table.
480
+
481
+ **Amortized cost analysis (Cornell CS, 2008):**
482
+ Each insertion is charged 3 units of work:
483
+ 1. 1 unit: insert the element now
484
+ 2. 2 units: "pre-pay" for future copying
485
+
486
+ When the table doubles from n/2 to n entries, exactly n/2 elements need
487
+ copying. The n/2 insertions since the last resize have each pre-paid 2 units,
488
+ providing exactly n units of work -- enough to copy n/2 elements (at 2 units
489
+ each for hash + place).
490
+
491
+ Result: amortized O(1) per insertion, even though individual resizes are O(n).
492
+
493
+ ### 6.3 Practical Implications
494
+
495
+ | Operation | Worst Case | Amortized | Structure |
496
+ |-------------------|------------|-----------|---------------------|
497
+ | ArrayList.add() | O(n) | O(1) | Dynamic array |
498
+ | HashMap.put() | O(n) | O(1) | Hash table |
499
+ | StringBuilder.append() | O(n) | O(1) | Dynamic char array |
500
+ | Splay tree access | O(n) | O(log n) | Self-adjusting BST |
501
+ | Union-Find | O(log n) | O(alpha(n)) ~= O(1) | Disjoint set forest |
502
+
503
+ **Why this matters in production:**
504
+ A monitoring system that triggers an alert when a single operation exceeds a
505
+ latency threshold (e.g., p99 > 50ms) will see occasional spikes from dynamic
506
+ array or hash table resizes. These are expected and correct. The fix is not to
507
+ avoid these data structures, but to:
508
+ 1. Pre-allocate with estimated capacity (e.g., `new HashMap<>(expectedSize)`)
509
+ 2. Set latency thresholds on amortized metrics, not individual operations
510
+ 3. Perform resizes during low-traffic periods if possible
511
+
512
+ Pre-allocation avoids resizing entirely. In Java, `new ArrayList<>(10000)`
513
+ vs `new ArrayList<>()` for a list that will hold 10,000 elements avoids ~14
514
+ resize-and-copy operations (10 -> 20 -> 40 -> ... -> 10240 -> 20480).
515
+
516
+ ---
517
+
518
+ ## 7. Common Bottleneck Patterns
519
+
520
+ ### 7.1 O(n^2) Hiding in Innocent-Looking Code
521
+
522
+ **Pattern 1: Method call hides linear scan**
523
+ ```python
524
+ # This looks O(n) but is O(n^2)
525
+ for item in items: # O(n)
526
+ if item in some_list: # O(n) -- list.__contains__ is linear
527
+ process(item)
528
+ ```
529
+
530
+ **Pattern 2: Sorting inside a loop**
531
+ ```python
532
+ # O(n^2 log n) -- sorts n times
533
+ for new_item in stream:
534
+ sorted_list.append(new_item)
535
+ sorted_list.sort() # O(n log n) each time
536
+ median = sorted_list[len(sorted_list) // 2]
537
+
538
+ # Fix: use heapq or bisect.insort for O(log n) per insertion
539
+ import bisect
540
+ for new_item in stream:
541
+ bisect.insort(sorted_list, new_item) # O(n) due to shift, but O(log n) search
542
+ median = sorted_list[len(sorted_list) // 2]
543
+
544
+ # Best: use a two-heap median structure for O(log n) per operation
545
+ ```
546
+
547
+ **Pattern 3: Repeated regex compilation**
548
+ ```python
549
+ # O(n * compile_cost) -- compiles regex n times
550
+ for line in lines:
551
+ if re.match(r'\d{4}-\d{2}-\d{2}', line):
552
+ process(line)
553
+
554
+ # Fix: compile once
555
+ pattern = re.compile(r'\d{4}-\d{2}-\d{2}')
556
+ for line in lines:
557
+ if pattern.match(line):
558
+ process(line)
559
+ ```
560
+ Python's re module caches the last ~512 patterns, but relying on this cache
561
+ in hot loops is fragile and adds hash-lookup overhead on every call.
562
+
563
+ **Pattern 4: N+1 query problem (databases)**
564
+ ```python
565
+ # O(n) queries -- 1 for users, then 1 per user for orders
566
+ users = db.query("SELECT * FROM users LIMIT 1000")
567
+ for user in users:
568
+ orders = db.query(f"SELECT * FROM orders WHERE user_id = {user.id}")
569
+
570
+ # Fix: O(1) queries with JOIN or IN clause
571
+ users_with_orders = db.query("""
572
+ SELECT u.*, o.* FROM users u
573
+ JOIN orders o ON u.id = o.user_id
574
+ WHERE u.id IN (SELECT id FROM users LIMIT 1000)
575
+ """)
576
+ ```
577
+ At 1,000 users with 2ms per query: BAD = 2,002 ms; GOOD = 4 ms (500x faster).
578
+
579
+ ### 7.2 Wrong Data Structure Selection
580
+
581
+ | Scenario | Wrong Choice | Right Choice | Impact at n=100K |
582
+ |------------------------------|--------------|--------------|------------------|
583
+ | Frequent membership checks | Array/List | HashSet | 43,000x slower |
584
+ | Priority queue operations | Sorted array | Binary heap | 100x slower insert |
585
+ | Frequency counting | List of pairs| HashMap | 100,000x slower |
586
+ | FIFO queue with size limit | Array (shift) | Ring buffer | 100,000x slower dequeue |
587
+ | Prefix matching | HashMap | Trie | 10x slower, more memory |
588
+ | Range queries on sorted data | Hash table | B-Tree/BST | Hash cannot do ranges |
589
+
590
+ ---
591
+
592
+ ## 8. Anti-Patterns: When NOT to Optimize
593
+
594
+ ### 8.1 Premature Optimization of Small n
595
+
596
+ Donald Knuth's full quote: "We should forget about small efficiencies, say
597
+ about 97% of the time: premature optimization is the root of all evil. Yet we
598
+ should not pass up our opportunities in that critical 3%."
599
+
600
+ **When n is small, constants dominate:**
601
+
602
+ | n | O(n^2) actual | O(n log n) actual | Winner |
603
+ |-----|---------------|-------------------|--------|
604
+ | 5 | 25 ops | ~12 ops + overhead | O(n^2) often faster |
605
+ | 10 | 100 ops | ~33 ops + overhead | Close / depends |
606
+ | 20 | 400 ops | ~86 ops + overhead | O(n log n) starts winning |
607
+ | 50 | 2,500 ops | ~282 ops | O(n log n) clearly wins |
608
+
609
+ For n < ~20, insertion sort (O(n^2)) typically beats quicksort (O(n log n))
610
+ because of lower constant factors: no recursion overhead, better branch
611
+ prediction, and superior cache locality. This is why every major standard
612
+ library sort falls back to insertion sort for small subarrays.
613
+
614
+ Source: Joe Duffy, "The 'premature optimization is evil' myth"
615
+ (https://joeduffyblog.com/2010/09/06/the-premature-optimization-is-evil-myth/)
616
+
617
+ ### 8.2 Ignoring Constants and Cache Effects
618
+
619
+ Big O ignores constant factors, but in practice they matter enormously:
620
+
621
+ **Example: Hash map vs. array for small lookups**
622
+ A hash map lookup is O(1), and array linear scan is O(n). But for n < ~10,
623
+ the array scan is faster because:
624
+ 1. Array data is contiguous in memory (1 cache line = 64 bytes = 16 ints)
625
+ 2. Hash computation has non-trivial constant cost (~20-50 ns)
626
+ 3. Hash map has pointer indirection (cache miss penalty: ~100 ns to main memory)
627
+
628
+ Benchmark (Dejan Gegic, Medium, 2024): scanning an array of 8 integers
629
+ takes ~3 ns; hash map lookup for the same data takes ~25 ns.
630
+
631
+ Source: Medium, "Why benchmarks are lying to you, and hash maps are not always
632
+ faster than arrays"
633
+ (https://medium.com/@dejangegic100/why-benchmarks-are-lying-to-you-and-hash-maps-are-not-always-faster-than-arrays-ac42cae649a7)
634
+
635
+ ### 8.3 Over-Engineering for Scale You Don't Have
636
+
637
+ **Anti-pattern**: Using a distributed B-tree index for 500 records.
638
+ **Reality**: A linear scan of 500 records takes ~50 us. The network round-trip
639
+ to a distributed index takes ~1 ms. The "worse" algorithm is 20x faster.
640
+
641
+ **Anti-pattern**: Implementing a custom Bloom filter for 100 items.
642
+ **Reality**: A HashSet of 100 items uses ~4 KB. The engineering cost of
643
+ maintaining custom probabilistic data structure code far exceeds the memory
644
+ savings.
645
+
646
+ **Rule of thumb**: If n will stay under 1,000 for the lifetime of the code,
647
+ the simplest correct solution is almost always the best. Optimize when
648
+ profiling shows a bottleneck, not when Big O analysis suggests one.
649
+
650
+ ### 8.4 What IS Worth Optimizing Early
651
+
652
+ Despite the above, some algorithmic choices are NOT premature optimization --
653
+ they are correctness of design:
654
+
655
+ 1. **Choosing a hash map over nested loops for lookups** -- this is a design
656
+ choice, not micro-optimization. The 43,000x difference at n=100K is not
657
+ a constant factor.
658
+
659
+ 2. **Using append + join instead of string concatenation** -- the quadratic
660
+ behavior is a bug, not a style preference.
661
+
662
+ 3. **Avoiding N+1 queries** -- this is architectural, and fixing it later
663
+ requires restructuring data access patterns.
664
+
665
+ 4. **Selecting appropriate database indexes** -- an unindexed query on a
666
+ table of 10M rows takes minutes; with an index, milliseconds.
667
+
668
+ Source: ACM Ubiquity, "The Fallacy of Premature Optimization"
669
+ (https://ubiquity.acm.org/article.cfm?id=1513451)
670
+
671
+ ---
672
+
673
+ ## 9. Before/After: Measured Improvements
674
+
675
+ ### 9.1 Case Study: User Search Deduplication
676
+
677
+ **Context**: A user-facing search feature deduplicating results across
678
+ multiple data sources.
679
+
680
+ ```python
681
+ # BEFORE: O(n * m) -- 45 seconds for 50,000 results across 5 sources
682
+ def deduplicate(result_lists):
683
+ final = []
684
+ for results in result_lists:
685
+ for item in results:
686
+ if item not in final: # O(n) scan of growing list
687
+ final.append(item)
688
+ return final
689
+
690
+ # AFTER: O(n + m) -- 12 ms for the same dataset
691
+ def deduplicate(result_lists):
692
+ seen = set()
693
+ final = []
694
+ for results in result_lists:
695
+ for item in results:
696
+ if item.id not in seen: # O(1) set lookup
697
+ seen.add(item.id)
698
+ final.append(item)
699
+ return final
700
+ ```
701
+
702
+ | Dataset Size | Before | After | Speedup |
703
+ |--------------|--------|-------|---------|
704
+ | 1,000 | 180 ms | 0.2 ms | 900x |
705
+ | 10,000 | 8.5 s | 1.8 ms | 4,700x |
706
+ | 50,000 | 45 s | 12 ms | 3,750x |
707
+
708
+ ### 9.2 Case Study: Log Processing Pipeline
709
+
710
+ **Context**: Processing server logs to extract unique IP addresses per hour.
711
+
712
+ ```python
713
+ # BEFORE: O(n^2) -- string concatenation + list search
714
+ def process_logs(log_lines):
715
+ report = ""
716
+ seen_ips = []
717
+ for line in log_lines:
718
+ ip = extract_ip(line)
719
+ if ip not in seen_ips: # O(n) list search
720
+ seen_ips.append(ip)
721
+ report += f"{line}\n" # O(n) string copy
722
+ return report, seen_ips
723
+
724
+ # AFTER: O(n) -- set + list join
725
+ def process_logs(log_lines):
726
+ seen_ips = set()
727
+ report_lines = []
728
+ for line in log_lines:
729
+ ip = extract_ip(line)
730
+ seen_ips.add(ip) # O(1)
731
+ report_lines.append(f"{line}") # O(1) amortized
732
+ return "\n".join(report_lines), seen_ips # O(n) single pass
733
+ ```
734
+
735
+ | Log Lines | Before | After | Speedup |
736
+ |-----------|---------|-------|---------|
737
+ | 10,000 | 2.1 s | 15 ms | 140x |
738
+ | 100,000 | 3.5 min | 150 ms | 1,400x |
739
+ | 1,000,000 | ~6 hrs | 1.5 s | ~14,400x|
740
+
741
+ The quadratic string concatenation alone accounts for ~80% of the slowdown
742
+ in the "before" version.
743
+
744
+ ### 9.3 Case Study: Event Matching System
745
+
746
+ **Context**: Matching incoming events against a set of subscription filters.
747
+
748
+ ```python
749
+ # BEFORE: O(events * subscriptions * fields) -- triple nested
750
+ def match_events(events, subscriptions):
751
+ matches = []
752
+ for event in events: # O(e)
753
+ for sub in subscriptions: # O(s)
754
+ match = True
755
+ for field, value in sub.filters.items(): # O(f)
756
+ if event.get(field) != value:
757
+ match = False
758
+ break
759
+ if match:
760
+ matches.append((event, sub))
761
+ return matches
762
+
763
+ # AFTER: O(events * fields) -- index subscriptions by first filter field
764
+ def match_events(events, subscriptions):
765
+ # Build index: field_value -> list of subscriptions
766
+ index = defaultdict(list)
767
+ for sub in subscriptions:
768
+ first_field = next(iter(sub.filters))
769
+ key = (first_field, sub.filters[first_field])
770
+ index[key].append(sub)
771
+
772
+ matches = []
773
+ for event in events:
774
+ for field, value in event.items():
775
+ for sub in index.get((field, value), []):
776
+ if all(event.get(f) == v for f, v in sub.filters.items()):
777
+ matches.append((event, sub))
778
+ return matches
779
+ ```
780
+
781
+ | Events | Subscriptions | Before | After | Speedup |
782
+ |--------|---------------|--------|-------|---------|
783
+ | 1,000 | 500 | 850 ms | 12 ms | 71x |
784
+ | 10,000 | 5,000 | 85 s | 120 ms | 708x |
785
+ | 100,000 | 50,000 | ~2.4 hrs| 1.8 s | ~4,800x |
786
+
787
+ ### 9.4 Case Study: Fibonacci -- Algorithm Choice Impact
788
+
789
+ | Algorithm | fib(30) | fib(40) | fib(50) |
790
+ |---------------------|-----------|-----------|-------------|
791
+ | Naive recursive | 12 ms | 1.2 s | 128 s |
792
+ | Memoized | 0.003 ms | 0.004 ms | 0.005 ms |
793
+ | Matrix exponentiation| 0.001 ms | 0.001 ms | 0.001 ms |
794
+
795
+ The naive approach is O(2^n); memoized is O(n); matrix exponentiation is
796
+ O(log n). At fib(50), the difference is 25 million fold.
797
+
798
+ ---
799
+
800
+ ## 10. Complexity Reduction Techniques Reference
801
+
802
+ ### 10.1 Common Reductions
803
+
804
+ | From | To | Technique | Example |
805
+ |------------|------------|----------------------------|-------------------------------|
806
+ | O(n^2) | O(n log n) | Sort + two pointers | Two-sum on sorted array |
807
+ | O(n^2) | O(n) | Hash map for lookups | Duplicate detection |
808
+ | O(n^2) | O(n) | Sliding window | Max sum subarray of size k |
809
+ | O(2^n) | O(n*W) | Dynamic programming | 0/1 Knapsack |
810
+ | O(n!) | O(n^2 * 2^n)| Bitmask DP | Traveling salesman (small n) |
811
+ | O(n) | O(log n) | Binary search | Search in sorted data |
812
+ | O(n) | O(1) | Precomputation / formula | Sum of 1..n = n(n+1)/2 |
813
+ | O(n log n) | O(n) | Counting/radix sort | Sort integers, small range |
814
+ | O(n*m) | O(n+m) | Set-based intersection | List deduplication |
815
+ | O(n^3) | O(n^2.37) | Strassen/fast matrix mult | Large matrix operations |
816
+
817
+ ### 10.2 The "Replace Inner Loop" Pattern
818
+
819
+ The single most impactful optimization pattern in everyday code:
820
+
821
+ 1. Identify the inner loop (the O(n) operation inside the O(n) loop)
822
+ 2. Replace it with an O(1) lookup using a hash-based structure
823
+ 3. Pay the O(n) cost once upfront to build the lookup structure
824
+
825
+ This pattern converts O(n^2) to O(n) and applies to:
826
+ - Membership testing (list -> set)
827
+ - Key-value lookup (list scan -> dict/map)
828
+ - Counting (nested loop -> counter/histogram)
829
+ - Grouping (filter loop -> group-by index)
830
+
831
+ ### 10.3 Complexity of Standard Library Operations
832
+
833
+ Engineers must know the complexity of the tools they use daily:
834
+
835
+ **Python:**
836
+ | Operation | Complexity | Notes |
837
+ |--------------------------|------------|-------------------------------|
838
+ | list.append() | O(1)* | Amortized |
839
+ | list.insert(0, x) | O(n) | Shifts all elements |
840
+ | list.pop() | O(1) | From end |
841
+ | list.pop(0) | O(n) | Shifts all elements |
842
+ | x in list | O(n) | Linear scan |
843
+ | x in set | O(1) | Hash lookup |
844
+ | dict[key] | O(1) | Hash lookup |
845
+ | list.sort() | O(n lg n) | Timsort |
846
+ | collections.deque.appendleft() | O(1) | Doubly-linked list |
847
+ | heapq.heappush() | O(log n) | Binary heap |
848
+ | bisect.insort() | O(n) | O(log n) search + O(n) shift |
849
+
850
+ **JavaScript:**
851
+ | Operation | Complexity | Notes |
852
+ |--------------------------|------------|-------------------------------|
853
+ | Array.push() | O(1)* | Amortized |
854
+ | Array.unshift() | O(n) | Shifts all elements |
855
+ | Array.includes() | O(n) | Linear scan |
856
+ | Set.has() | O(1) | Hash lookup |
857
+ | Map.get() | O(1) | Hash lookup |
858
+ | Array.sort() | O(n lg n) | Timsort (V8) |
859
+ | Array.indexOf() | O(n) | Linear scan |
860
+ | Array.splice(0, 1) | O(n) | Shifts all elements |
861
+ | Object property access | O(1) | Hash lookup (avg) |
862
+
863
+ ---
864
+
865
+ ## 11. Profiling and Measurement Checklist
866
+
867
+ Before optimizing, measure. After optimizing, measure again.
868
+
869
+ ### 11.1 Measurement Protocol
870
+
871
+ 1. **Establish a baseline**: Run the code at realistic data sizes (not just
872
+ unit-test sizes). Record wall time, CPU time, and memory usage.
873
+
874
+ 2. **Apply the doubling test**: Run at n, 2n, 4n, 8n. Plot the results.
875
+ The curve shape reveals the complexity class.
876
+
877
+ 3. **Profile, don't guess**: Use language-specific profilers:
878
+ - Python: cProfile, py-spy, line_profiler
879
+ - JavaScript/Node: --prof flag, clinic.js, 0x
880
+ - Java: JMH (microbenchmarks), async-profiler
881
+ - Go: pprof (built-in)
882
+ - Rust: criterion (benchmarks), flamegraph
883
+
884
+ 4. **Isolate the hot path**: The top 1-3 functions by cumulative time are
885
+ where optimization effort should focus. Amdahl's Law: speeding up 5%
886
+ of execution by 100x yields only ~4.8% total improvement.
887
+
888
+ 5. **Measure at production scale**: An algorithm that takes 1ms at n=100
889
+ may take 100 seconds at n=100,000. Test at the scale your system will
890
+ actually operate at, plus a 10x margin.
891
+
892
+ ### 11.2 Complexity Estimation from Empirical Data
893
+
894
+ | Observed Pattern (doubling n) | Likely Complexity |
895
+ |-------------------------------|-------------------|
896
+ | Time stays constant | O(1) |
897
+ | Time increases by constant | O(log n) |
898
+ | Time doubles | O(n) |
899
+ | Time slightly more than doubles | O(n log n) |
900
+ | Time quadruples | O(n^2) |
901
+ | Time octuples | O(n^3) |
902
+ | Time grows explosively | O(2^n) or worse |
903
+
904
+ ---
905
+
906
+ ## 12. Quick Reference Card
907
+
908
+ ### The Five Questions Before Choosing a Data Structure
909
+
910
+ 1. **What is n today?** And what will n be in 2 years?
911
+ 2. **What operations dominate?** Insert, lookup, delete, iterate, range query?
912
+ 3. **Is ordering required?** Sorted traversal, min/max, nearest neighbor?
913
+ 4. **What are the memory constraints?** Embedded system vs. cloud server?
914
+ 5. **Is thread safety needed?** Concurrent access patterns?
915
+
916
+ ### The Three Laws of Practical Complexity
917
+
918
+ **Law 1: Measure before you optimize.**
919
+ A profiler tells you where the time goes. Big O tells you how it will grow.
920
+ Use both.
921
+
922
+ **Law 2: The right data structure beats the right algorithm.**
923
+ Swapping a list for a set (changing the data structure) is simpler and more
924
+ impactful than inventing a clever algorithm to search a list faster.
925
+
926
+ **Law 3: Constants matter until n is large.**
927
+ At n = 10, a 50ns hash computation costs more than scanning 10 contiguous
928
+ integers (~3ns). At n = 10,000, the hash lookup is 43,000x faster than
929
+ the scan. Know your n.
930
+
931
+ ---
932
+
933
+ ## Sources
934
+
935
+ - Baeldung: "Java TreeMap vs HashMap" -- https://www.baeldung.com/java-treemap-vs-hashmap
936
+ - Baeldung: "Time Complexity of Java Collections" -- https://www.baeldung.com/java-collections-complexity
937
+ - DEV Community: "Why Set.has() is Faster Than Array.includes()" -- https://dev.to/programminginblood/why-sethas-is-faster-than-arrayincludes-for-finding-items-4hh
938
+ - Medium (Dejan Gegic): "Why benchmarks are lying to you, and hash maps are not always faster than arrays" -- https://medium.com/@dejangegic100/why-benchmarks-are-lying-to-you-and-hash-maps-are-not-always-faster-than-arrays-ac42cae649a7
939
+ - Medium (Yuri Lima): "JavaScript Performance: When Set Becomes Faster Than Array.includes()" -- https://medium.com/@yuri-lima/javascript-performance-when-set-becomes-faster-than-array-includes-0adb3134f95b
940
+ - Cornell CS3110: "Amortized analysis and dynamic tables" -- https://www.cs.cornell.edu/courses/cs3110/2009sp/lectures/lec21.html
941
+ - Cornell CS3110: "Hash tables and amortized analysis" -- https://www.cs.cornell.edu/courses/cs3110/2008fa/lectures/lec22_amort.html
942
+ - OpenDSA: "An Empirical Comparison of Sorting Algorithms" -- https://opendsa-server.cs.vt.edu/ODSA/Books/Everything/html/SortingEmpirical.html
943
+ - GeeksforGeeks: "Complete Guide On Complexity Analysis" -- https://www.geeksforgeeks.org/dsa/complete-guide-on-complexity-analysis/
944
+ - PyPy Blog: "Repeated string concatenation is quadratic" -- https://pypy.org/posts/2023/01/string-concatenation-quadratic.html
945
+ - Nelson Elhage: "Accidentally Quadratic" -- https://accidentallyquadratic.tumblr.com
946
+ - Julia Evans: "Quadratic algorithms are slow (and hashmaps are fast)" -- https://jvns.ca/blog/2021/09/10/hashmaps-make-things-fast/
947
+ - Joe Duffy: "The 'premature optimization is evil' myth" -- https://joeduffyblog.com/2010/09/06/the-premature-optimization-is-evil-myth/
948
+ - ACM Ubiquity: "The Fallacy of Premature Optimization" -- https://ubiquity.acm.org/article.cfm?id=1513451
949
+ - Daniel Lemire: "Big-O notation and real-world performance" -- https://lemire.me/blog/2013/07/11/big-o-notation-and-real-world-performance/
950
+ - AWS: "Bloom filters in Amazon ElastiCache" -- https://aws.amazon.com/blogs/database/implement-fast-space-efficient-lookups-using-bloom-filters-in-amazon-elasticache/
951
+ - Wikipedia: "Bloom filter" -- https://en.wikipedia.org/wiki/Bloom_filter
952
+ - freeCodeCamp: "Big O Cheat Sheet" -- https://www.freecodecamp.org/news/big-o-cheat-sheet-time-complexity-chart/
953
+ - Scattered Thoughts: "Smolderingly fast b-trees" -- https://www.scattered-thoughts.net/writing/smolderingly-fast-btrees/
954
+ - AWS CodeGuru: "Inefficient string concatenation inside loop" -- https://docs.aws.amazon.com/codeguru/detector-library/python/string-concatenation/