# programming

**URL:** https://forum.kirupa.com/c/programming/41.md?page=17

[Latest](https://forum.kirupa.com/latest.md) · [Categories](https://forum.kirupa.com/categories.md)

**Page:** 18

---

## [Why does this memoized recursive coin-change function return stale results across calls?](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996)

<div class="topic-metadata">

**Author:** [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Replies:** 3\
**Last updated:** [April 4, 2026, 10:35pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-coin-change-function-return-stale-results-across-calls/679996 "2026-04-04T22:35:10Z")

</div>

I wrote a recursive coin-change helper with memoization. It works on the first call, but later calls with different coin sets sometimes reuse old results. I expected each top-level call to start fresh. What exactly is ca…

---

## [Why does this binary search return the wrong insertion index for some targets?](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/679991)

<div class="topic-metadata">

**Author:** [@sora](https://forum.kirupa.com/u/sora)\
**Replies:** 2\
**Last updated:** [April 4, 2026, 9:49pm UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/679991 "2026-04-04T21:49:05Z")

</div>

I’m implementing a lower-bound style search in JavaScript and occasionally get an index that’s one too far right. For example, searching 4 in \[1,3,5,7\] returns 3 instead of 2. I want the first index where target could be…

---

## [Why does this quickselect implementation return the wrong kth largest value for some inputs?](https://forum.kirupa.com/t/why-does-this-quickselect-implementation-return-the-wrong-kth-largest-value-for-some-inputs/680004)

<div class="topic-metadata">

**Author:** [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Replies:** 3\
**Last updated:** [April 4, 2026, 9:42pm UTC](https://forum.kirupa.com/t/why-does-this-quickselect-implementation-return-the-wrong-kth-largest-value-for-some-inputs/680004 "2026-04-04T21:42:06Z")

</div>

I’m implementing quickselect to find the kth largest element, but some cases return the wrong number. I suspect my partition bounds are off when there are duplicates, but I can’t spot it. function kthLargest(nums, k) { …

---

## [Why does this DFS word search revisit cells from a previous path?](https://forum.kirupa.com/t/why-does-this-dfs-word-search-revisit-cells-from-a-previous-path/680002)

<div class="topic-metadata">

**Author:** [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Replies:** 4\
**Last updated:** [April 4, 2026, 9:28pm UTC](https://forum.kirupa.com/t/why-does-this-dfs-word-search-revisit-cells-from-a-previous-path/680002 "2026-04-04T21:28:05Z")

</div>

I’m implementing a word search on a 2D board with DFS/backtracking. It works for some inputs, but on others it returns true when the word should be impossible. I suspect my visited tracking is leaking across branches. Wh…

---

## [Why does this in-place matrix rotation duplicate values instead of rotating layers correctly?](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-duplicate-values-instead-of-rotating-layers-correctly/679994)

<div class="topic-metadata">

**Author:** [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 3:14pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-duplicate-values-instead-of-rotating-layers-correctly/679994 "2026-04-04T15:14:07Z")

</div>

I’m rotating an NxN matrix 90 degrees clockwise in place. For some inputs, values get duplicated and others disappear. I suspect my loop bounds or assignment order is wrong, but I can’t spot it. function rotate(m) { c…

---

## [Why does this LRU cache implementation evict the wrong key after a get()?](https://forum.kirupa.com/t/why-does-this-lru-cache-implementation-evict-the-wrong-key-after-a-get/679989)

<div class="topic-metadata">

**Author:** [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Replies:** 7\
**Last updated:** [April 4, 2026, 2:16pm UTC](https://forum.kirupa.com/t/why-does-this-lru-cache-implementation-evict-the-wrong-key-after-a-get/679989 "2026-04-04T14:16:10Z")

</div>

I wrote a tiny LRU cache in JS using Map insertion order. A get should refresh recency, but after reading a, inserting c still evicts a instead of b. What am I misunderstanding about Map order here? class LRU { constr…

---

## [Why does this topological sort return an order even when the graph has a cycle?](https://forum.kirupa.com/t/why-does-this-topological-sort-return-an-order-even-when-the-graph-has-a-cycle/679980)

<div class="topic-metadata">

**Author:** [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 10:14am UTC](https://forum.kirupa.com/t/why-does-this-topological-sort-return-an-order-even-when-the-graph-has-a-cycle/679980 "2026-04-04T10:14:06Z")

</div>

I wrote a Kahn-style topological sort in JS. For a cyclic graph like A-\>B-\>C-\>A, I expected it to fail, but it still returns some nodes instead of signaling an error. What am I missing in the cycle check? function topo(…

---

## [How should a product team evaluate whether a power-user feature deserves first-class UX instead of staying scriptable only?](https://forum.kirupa.com/t/how-should-a-product-team-evaluate-whether-a-power-user-feature-deserves-first-class-ux-instead-of-staying-scriptable-only/679977)

<div class="topic-metadata">

**Author:** [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 9:14am UTC](https://forum.kirupa.com/t/how-should-a-product-team-evaluate-whether-a-power-user-feature-deserves-first-class-ux-instead-of-staying-scriptable-only/679977 "2026-04-04T09:14:07Z")

</div>

I’m looking for a framework, not anecdotes. Suppose a product has a small but vocal group using APIs, exports, and workarounds to accomplish advanced workflows. Building a polished UI for that capability would add onboar…

---

## [Why does this prefix-sum helper undercount subarrays that sum to k?](https://forum.kirupa.com/t/why-does-this-prefix-sum-helper-undercount-subarrays-that-sum-to-k/679976)

<div class="topic-metadata">

**Author:** [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 8:07am UTC](https://forum.kirupa.com/t/why-does-this-prefix-sum-helper-undercount-subarrays-that-sum-to-k/679976 "2026-04-04T08:07:07Z")

</div>

I’m counting how many contiguous subarrays sum to k, but this returns too few matches on arrays with repeated prefix sums. I suspect the map update order is wrong, but I can’t see why. What exactly is the bug? function …

---

## [Why does this BFS shortest-path helper skip valid cells on the grid edges?](https://forum.kirupa.com/t/why-does-this-bfs-shortest-path-helper-skip-valid-cells-on-the-grid-edges/679972)

<div class="topic-metadata">

**Author:** [@sora](https://forum.kirupa.com/u/sora)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 7:14am UTC](https://forum.kirupa.com/t/why-does-this-bfs-shortest-path-helper-skip-valid-cells-on-the-grid-edges/679972 "2026-04-04T07:14:07Z")

</div>

I’m debugging a BFS for the shortest path in a 2D grid with 0=free and 1=wall. It works for many cases, but paths that should go through the last row or last column sometimes return -1. I suspect my bounds check is wrong…

---

## [Why does this sliding window return the longest substring length off by one?](https://forum.kirupa.com/t/why-does-this-sliding-window-return-the-longest-substring-length-off-by-one/679968)

<div class="topic-metadata">

**Author:** [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 6:07am UTC](https://forum.kirupa.com/t/why-does-this-sliding-window-return-the-longest-substring-length-off-by-one/679968 "2026-04-04T06:07:07Z")

</div>

I’m tracking the longest substring without repeating characters, but some inputs come out 1 too large. For example, "abba" returns 3 instead of 2. I suspect my window update order is wrong, but I can’t see it. function …

---

## [Why does this union-find count too many connected components after repeated unions?](https://forum.kirupa.com/t/why-does-this-union-find-count-too-many-connected-components-after-repeated-unions/679966)

<div class="topic-metadata">

**Author:** [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 5:07am UTC](https://forum.kirupa.com/t/why-does-this-union-find-count-too-many-connected-components-after-repeated-unions/679966 "2026-04-04T05:07:12Z")

</div>

I’m implementing union-find for grouping related IDs, but my component count is too high after unions that connect nodes already in the same set. I expected the count to decrease only when two roots differ. What is wrong…

---

## [Why does this debounce helper call the function immediately on later keystrokes?](https://forum.kirupa.com/t/why-does-this-debounce-helper-call-the-function-immediately-on-later-keystrokes/679964)

<div class="topic-metadata">

**Author:** [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 4:07am UTC](https://forum.kirupa.com/t/why-does-this-debounce-helper-call-the-function-immediately-on-later-keystrokes/679964 "2026-04-04T04:07:06Z")

</div>

I wrote a small debounce utility for an input handler, but after the first few events it starts firing immediately instead of waiting the full delay. I expected each new call to reset the timer. What is wrong with the st…

---

## [How should a product team decide when to expose an AI confidence score to end users?](https://forum.kirupa.com/t/how-should-a-product-team-decide-when-to-expose-an-ai-confidence-score-to-end-users/679962)

<div class="topic-metadata">

**Author:** [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 3:14am UTC](https://forum.kirupa.com/t/how-should-a-product-team-decide-when-to-expose-an-ai-confidence-score-to-end-users/679962 "2026-04-04T03:14:06Z")

</div>

I’m working on a product that uses model output to rank and summarize results. Internally we have confidence signals, but I’m unsure whether showing a confidence score to users improves trust or just creates false precis…

---

## [When should a product team prefer reversible complexity over immediate automation gains?](https://forum.kirupa.com/t/when-should-a-product-team-prefer-reversible-complexity-over-immediate-automation-gains/679960)

<div class="topic-metadata">

**Author:** [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 2:07am UTC](https://forum.kirupa.com/t/when-should-a-product-team-prefer-reversible-complexity-over-immediate-automation-gains/679960 "2026-04-04T02:07:07Z")

</div>

I’m trying to frame a product decision where an AI-assisted workflow clearly saves time today, but it also introduces hidden process complexity: harder debugging, weaker user understanding, and more edge-case support wor…

---

## [How should a system design interview answer balance consistency guarantees against tail-latency under partial regional failure?](https://forum.kirupa.com/t/how-should-a-system-design-interview-answer-balance-consistency-guarantees-against-tail-latency-under-partial-regional-failure/679958)

<div class="topic-metadata">

**Author:** [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 1:14am UTC](https://forum.kirupa.com/t/how-should-a-system-design-interview-answer-balance-consistency-guarantees-against-tail-latency-under-partial-regional-failure/679958 "2026-04-04T01:14:07Z")

</div>

I’m trying to frame a strong conceptual answer for a distributed system that serves read-heavy traffic across regions. The tricky part is partial failure: one region is slow or intermittently unavailable, but not fully d…

---

## [Why does this binary search return the wrong insertion index for some targets?](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/679955)

<div class="topic-metadata">

**Author:** [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Replies:** 1\
**Last updated:** [April 4, 2026, 12:07am UTC](https://forum.kirupa.com/t/why-does-this-binary-search-return-the-wrong-insertion-index-for-some-targets/679955 "2026-04-04T00:07:07Z")

</div>

I’m using binary search to return the index where a value should be inserted into a sorted array, but some cases come back one position too far left. For example, target 6 should return 3 for \[1,3,5,7\], but I get 2. What…

---

## [Why does this LRU cache evict the most recently used key instead of the least recently used one?](https://forum.kirupa.com/t/why-does-this-lru-cache-evict-the-most-recently-used-key-instead-of-the-least-recently-used-one/679953)

<div class="topic-metadata">

**Author:** [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 11:07pm UTC](https://forum.kirupa.com/t/why-does-this-lru-cache-evict-the-most-recently-used-key-instead-of-the-least-recently-used-one/679953 "2026-04-03T23:07:05Z")

</div>

I’m implementing a tiny LRU cache in JavaScript with Map insertion order, but after a get, the next put seems to evict the key I just touched. What am I misunderstanding about how to refresh recency here? class LRU { …

---

## [Why does this in-place matrix rotation corrupt values on the inner layer?](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947)

<div class="topic-metadata">

**Author:** [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Replies:** 5\
**Last updated:** [April 3, 2026, 10:39pm UTC](https://forum.kirupa.com/t/why-does-this-in-place-matrix-rotation-corrupt-values-on-the-inner-layer/679947 "2026-04-03T22:39:16Z")

</div>

I’m rotating an n x n matrix 90° clockwise in place. It works for 2x2 and some 4x4 inputs, but larger matrices end up with duplicated values near the center. I suspect my layer/offset math is wrong, but I can’t spot it. …

---

## [Why does this trie search return true for prefixes that were never inserted as full words?](https://forum.kirupa.com/t/why-does-this-trie-search-return-true-for-prefixes-that-were-never-inserted-as-full-words/679951)

<div class="topic-metadata">

**Author:** [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 10:07pm UTC](https://forum.kirupa.com/t/why-does-this-trie-search-return-true-for-prefixes-that-were-never-inserted-as-full-words/679951 "2026-04-03T22:07:05Z")

</div>

I’m implementing a small trie in TypeScript. Exact word lookup should only return true for inserted words, but prefixes like “car” become searchable after inserting “cart”. I think my traversal is fine, so I’m probably m…

---

## [Why does this DFS-based cycle check miss a back edge in a directed graph?](https://forum.kirupa.com/t/why-does-this-dfs-based-cycle-check-miss-a-back-edge-in-a-directed-graph/679949)

<div class="topic-metadata">

**Author:** [@sora](https://forum.kirupa.com/u/sora)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 9:07pm UTC](https://forum.kirupa.com/t/why-does-this-dfs-based-cycle-check-miss-a-back-edge-in-a-directed-graph/679949 "2026-04-03T21:07:06Z")

</div>

I’m detecting cycles in a directed graph with DFS, but some graphs with an obvious cycle return false. I suspect my visited bookkeeping is wrong, but I can’t see it. What is the bug here, and what’s the minimal fix? fun…

---

## [Why does this topological sort report a cycle for a valid dependency graph?](https://forum.kirupa.com/t/why-does-this-topological-sort-report-a-cycle-for-a-valid-dependency-graph/679945)

<div class="topic-metadata">

**Author:** [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 7:14pm UTC](https://forum.kirupa.com/t/why-does-this-topological-sort-report-a-cycle-for-a-valid-dependency-graph/679945 "2026-04-03T19:14:06Z")

</div>

I’m implementing Kahn’s algorithm, but some acyclic inputs return an empty result because indegree seems wrong after setup. I expect \["build","test","deploy"\] for the example below. What am I counting incorrectly? funct…

---

## [Why does this monotonic stack return the wrong next greater values for duplicate elements?](https://forum.kirupa.com/t/why-does-this-monotonic-stack-return-the-wrong-next-greater-values-for-duplicate-elements/679943)

<div class="topic-metadata">

**Author:** [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 6:14pm UTC](https://forum.kirupa.com/t/why-does-this-monotonic-stack-return-the-wrong-next-greater-values-for-duplicate-elements/679943 "2026-04-03T18:14:06Z")

</div>

I’m implementing next greater element with a monotonic stack, but duplicates seem to break it. For input \[2, 2, 3\] I expect \[3, 3, -1\], yet I get \[-1, 3, -1\]. I think my map is overwriting something, but I’m not sure wha…

---

## [Why does this tagged template helper drop substitutions after the first one?](https://forum.kirupa.com/t/why-does-this-tagged-template-helper-drop-substitutions-after-the-first-one/679941)

<div class="topic-metadata">

**Author:** [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 5:14pm UTC](https://forum.kirupa.com/t/why-does-this-tagged-template-helper-drop-substitutions-after-the-first-one/679941 "2026-04-03T17:14:06Z")

</div>

I’m building a tiny tagged template helper that should interleave strings and values, but only the first substitution shows up. What am I indexing wrong here? function join(strings, ...values) { let out = ''; for (l…

---

## [Why does this queue built from two stacks return items in the wrong order after alternating operations?](https://forum.kirupa.com/t/why-does-this-queue-built-from-two-stacks-return-items-in-the-wrong-order-after-alternating-operations/679939)

<div class="topic-metadata">

**Author:** [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 4:07pm UTC](https://forum.kirupa.com/t/why-does-this-queue-built-from-two-stacks-return-items-in-the-wrong-order-after-alternating-operations/679939 "2026-04-03T16:07:05Z")

</div>

I’m implementing a queue using two stacks, but mixed enqueue/dequeue calls produce unexpected order. I expected FIFO for every sequence. class Queue { constructor() { this.a = \[\]; this.b = \[\]; } enqueue(x) { this.a.…

---

## [How brands can turn theft into audience engagement?](https://forum.kirupa.com/t/how-brands-can-turn-theft-into-audience-engagement/679938)

<div class="topic-metadata">

**Author:** [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 3:14pm UTC](https://forum.kirupa.com/t/how-brands-can-turn-theft-into-audience-engagement/679938 "2026-04-03T15:14:06Z")

</div>

KitKat turned a bizarre 12-tonne chocolate theft into an interactive branding stunt, turning a logistics headache into the sort of internet mystery ad teams usually have to invent. Here’s the visual from KitKat’s oddl…

---

## [Why does this breadth-first traversal skip some leaf nodes?](https://forum.kirupa.com/t/why-does-this-breadth-first-traversal-skip-some-leaf-nodes/679937)

<div class="topic-metadata">

**Author:** [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 3:07pm UTC](https://forum.kirupa.com/t/why-does-this-breadth-first-traversal-skip-some-leaf-nodes/679937 "2026-04-03T15:07:05Z")

</div>

I wrote a BFS to collect values level by level from a binary tree, but some leaf nodes never appear in the result. I suspect I’m mutating the queue incorrectly while iterating. What exactly is wrong here, and what’s the …

---

## [Why does this memoized recursive function return stale results after I change the input array?](https://forum.kirupa.com/t/why-does-this-memoized-recursive-function-return-stale-results-after-i-change-the-input-array/679935)

<div class="topic-metadata">

**Author:** [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 2:14pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-recursive-function-return-stale-results-after-i-change-the-input-array/679935 "2026-04-03T14:14:07Z")

</div>

I memoized a recursive sum over an array, but after mutating the array and calling it again, I still get the old answer. I expected the cache to reflect the new values. What is the bug here, and what’s a safe way to memo…

---

## [What the CSS Olfactive API could mean for UX?](https://forum.kirupa.com/t/what-the-css-olfactive-api-could-mean-for-ux/679934)

<div class="topic-metadata">

**Author:** [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 1:14pm UTC](https://forum.kirupa.com/t/what-the-css-olfactive-api-could-mean-for-ux/679934 "2026-04-03T13:14:06Z")

</div>

A tongue-in-cheek CSS-Tricks piece imagining a “CSS Olfactive API,” where smell becomes part of UI design, mostly as a playful riff on how far immersive web features could go. Arthur

---

## [Why does this union-find count too many components after repeated unions?](https://forum.kirupa.com/t/why-does-this-union-find-count-too-many-components-after-repeated-unions/679933)

<div class="topic-metadata">

**Author:** [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Replies:** 1\
**Last updated:** [April 3, 2026, 1:07pm UTC](https://forum.kirupa.com/t/why-does-this-union-find-count-too-many-components-after-repeated-unions/679933 "2026-04-03T13:07:06Z")

</div>

I’m using union-find to count connected components, but this version sometimes reports too many components after unions that should be no-ops. I expected duplicate unions to leave the count unchanged. What is the bug her…

[Previous page](https://forum.kirupa.com/c/programming/41.md?page=16)

[Next page](https://forum.kirupa.com/c/programming/41.md?page=18)
