Skip to content

fix: repair all article solutions that deterministically fail on the NC judge - #6051

Open
neetcode-gh wants to merge 5 commits into
mainfrom
fix/judge-article-failures
Open

fix: repair all article solutions that deterministically fail on the NC judge#6051
neetcode-gh wants to merge 5 commits into
mainfrom
fix/judge-article-failures

Conversation

@neetcode-gh

Copy link
Copy Markdown
Owner

Summary

A 4-runs-per-combo soak of every article approach × language across 72 problems against the live judge surfaced a set of deterministic (4/4) failures. This PR fixes all of them on the article side. It includes #6039 (Go stdlib heap/stack rewrites) and #6044 (pow-x-n JS INT_MIN fix) as merges — merging this PR marks both as merged — and adds one commit on top:

  • time-based-key-value-store — Brute Force, all 9 languages: a value set at timestamp 0 could never be returned; the seen sentinel of 0 collided with a legal timestamp (site contract is 0 <= timestamp <= 10^7). Sentinel is now -1. The C# tab also now takes the max qualifying timestamp like every other tab instead of relying on dictionary insertion order.
  • palindrome-partitioning — Rust Recursion and Backtracking (DP): the palindrome-table fill computed i + l - 2, which underflows usize at l = 1, i = 0 and panics at runtime on every input (the judge saw empty output). Now computes j = i + l - 1 guarded by l <= 2.
  • copy-linked-list-with-random-pointer — all 5 Rust tabs: replaced the index-based placeholder stubs ("LeetCode does not support Rust") with real implementations against the judge's Option<Rc<RefCell<Node>>> type, including both O(1) extra-space interleaving variants.
  • kth-smallest-integer-in-bst — Kotlin Morris Traversal: assigned through nullable receivers and did not compile; rewritten null-safely. Go/Kotlin Brute Force: these tabs did an inorder collect with no sort — a different algorithm from the python/java tabs under the same heading; now collect + sort like the rest.
  • longest-substring-without-duplicates: the Swift Brute Force snippet was fenced as ```kotlin, so kotlin appeared twice and swift had zero coverage.

Verification

Every touched problem was run twice (three times for time-based-key-value-store) through test-problem-article-solutions.ts against the live judge — 20 problems, 41 runs total. All previously-failing deterministic combos now pass. The only remaining failures are the known pre-existing time-limit tier (Brute Force TLEs) and the kth-largest Quick Select python stress-case issue tracked separately on the site side.

Companion PR on the site repo enriches four judge drivers (swift Node: Hashable, kotlin/swift 3-arg TreeNode, swift 2-arg ListNode) that three of these fixes rely on.

🤖 Generated with Claude Code

neetcode-gh and others added 5 commits August 1, 2026 09:14
Article Go snippets are spliced into a judge driver that already declares
`package main` and its own import block, so a snippet can neither add an
import nor pull in a third-party module. Every Go solution that used
github.com/emirpasic/gods therefore failed to compile on the judge.

Rewrites all 28 affected tabs to the standard library only:

  - priorityqueue.Queue  -> container/heap with a local sort.Interface type
  - linkedliststack      -> plain slice used as a stack
  - arrayqueue /
    linkedlistqueue      -> plain slice used as a queue
  - redblacktree /
    treemap              -> count map + sorted slice, binary searched with
                            sort.Search / sort.SearchInts

The drivers already provide container/heap, sort, math and friends
ambiently, so none of the new snippets declare imports. Published
structure and naming are kept as close to the originals as possible.

Also removes a stray `import "container/heap"` from the
design-a-leaderboard "Heap for top-K" tab, which failed to compile for the
same reason (redeclared / imported and not used against the driver block).

Behaviour is preserved throughout. Two latent bugs disappear as a side
effect: hand-of-straights now bounds-checks the heap before peeking, and
design-a-leaderboard Reset no longer decrements the count for score 0 when
the player is absent.

Every rewritten tab was run against the production judge
(test-problem-article-solutions.ts --lang go); all 29 pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The Binary Exponentiation (Iterative) JavaScript snippet was the only one of
the article's nine languages that ran the bit loop on a value that does not
fit in a signed 32-bit integer.

`Math.abs(-2147483648)` correctly produces `2147483648` (JS numbers are
doubles), but `&` and `>>=` apply ToInt32 to their operands first. So
`power & 1` evaluates `-2147483648 & 1` -> `0` (the multiply is skipped) and
`power >>= 1` evaluates `-2147483648 >> 1` -> `-1073741824`, which fails the
`power > 0` guard. The loop exits after a single iteration with `res` still 1.

On LeetCode's own published test case `x = 2.00000, n = -2147483648`
(expected `0.00000`) the snippet returned `1`.

Every other language already widens to 64 bits before the loop --
`Math.abs((long)n)` in Java/C#, `abs((long)n)` in C++, `(n as i64).abs()` in
Rust, `n.toLong()` in Kotlin, 64-bit `Int`/`int` in Swift/Go, and arbitrary
precision in Python -- so none of them were affected. The article's own
"Integer Overflow When Negating n" pitfall section documents exactly this
trap.

Fixed by using arithmetic instead of bitwise operations, which keeps `power`
a full-precision double: `power % 2 === 1` and `Math.floor(power / 2)`. The
recursive JavaScript solution already did this and was correct. Also extended
the pitfall section with the JavaScript-specific form of the trap.

Verified against the NeetCode judge: all 9 languages pass both binary
exponentiation approaches, including an `x = 2.00000, n = -2147483648` case
that the previous JavaScript code failed with Wrong Answer.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- time-based-key-value-store Brute Force (all 9 languages): a value set at
  timestamp 0 was unreachable — the 'seen' sentinel collided with a legal
  timestamp. Use -1 (timestamps are >= 0 on the site). Also make the C# tab
  take the max like every other tab instead of relying on insertion order.
- palindrome-partitioning rust Recursion + Backtracking (DP): 'i + l - 2'
  underflows usize when l = 1, i = 0, panicking at runtime on every input.
  Compute j = i + l - 1 and guard with l <= 2.
- copy-linked-list-with-random-pointer rust (all 5 tabs): replace the
  index-based placeholder stubs ('LeetCode does not support Rust') with real
  implementations against Option<Rc<RefCell<Node>>>, including both O(1)
  extra-space variants.
- kth-smallest-integer-in-bst: kotlin Morris assigned through nullable
  receivers and did not compile — rewritten null-safely. The go and kotlin
  Brute Force tabs did an inorder collect (no sort), a different algorithm
  from the python/java tabs under the same heading — now collect + sort.
- longest-substring-without-duplicates: the swift Brute Force snippet was
  fenced as kotlin, so kotlin appeared twice and swift had no coverage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@neetcode-gh

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@neetcode-gh

Copy link
Copy Markdown
Owner Author

@greptileai review

@neetcode-gh

Copy link
Copy Markdown
Owner Author

bugbot run

@cursor

cursor Bot commented Aug 4, 2026

Copy link
Copy Markdown

Skipping Bugbot: Bugbot is disabled for this repository. Visit the Bugbot dashboard to update your settings.

@neetcode-gh

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@neetcode-gh

Copy link
Copy Markdown
Owner Author

@greptileai review

@neetcode-gh

Copy link
Copy Markdown
Owner Author

Review loop tally: CodeRabbit and Greptile were triggered twice (~18:48Z and ~18:36Z cycles) and stayed silent; neither app has ever reviewed a PR in this repo (checked #6039/#6044 history) and Bugbot is disabled here, so bot review is not available on this repo. Verification instead ran against the live judge: 20 problems × 2 runs each via test-problem-article-solutions.ts — every previously-failing deterministic approach×language combo now passes; remaining failures are the pre-existing Brute Force time-limit tier only. Ready for review/merge — merging this PR also marks #6039 and #6044 as merged (their branches are included).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant