Skip to content

fix(go): replace emirpasic/gods with Go stdlib in article solutions - #6039

Open
neetcode-gh wants to merge 1 commit into
mainfrom
fix/go-stdlib-rewrites
Open

fix(go): replace emirpasic/gods with Go stdlib in article solutions#6039
neetcode-gh wants to merge 1 commit into
mainfrom
fix/go-stdlib-rewrites

Conversation

@neetcode-gh

Copy link
Copy Markdown
Owner

Problem

Article Go solutions that used github.com/emirpasic/gods cannot run on the judge.

On the platform, an article's Go snippet is spliced into a per-problem driver that already declares package main and owns the entire import block:

package main

import (
    "bufio"; "fmt"; "os"; "strings"
    "math"; "sort"; "strconv"; "regexp"
    "container/heap"; "container/list"
    "math/rand"; "math/bits"; "unicode"; "bytes"
    "encoding/json"; "runtime/debug"
)

[userCodeGoesHere]

A snippet therefore cannot add an import, and a third-party module could not be resolved even if it could. Every gods-based Go tab failed to compile.

An earlier approach — teaching the drivers to import gods — was abandoned. Rewriting to the standard library is the maintainer-confirmed fix.

Change

All 28 affected Go tabs rewritten to stdlib only:

gods construct stdlib replacement
priorityqueue.NewWith(...) container/heap + 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

Because the driver already provides container/heap, sort, math and the rest ambiently, none of the new snippets declare imports. Published structure and naming are kept as close to the originals as possible, and behaviour is preserved throughout.

Also removes a stray import "container/heap" from the design-a-leaderboard Heap for top-K tab — not a gods tab, but broken on the judge for exactly the same reason (heap redeclared in this block / "container/heap" imported and not used).

Two latent bugs disappear as a side effect:

  • hand-of-straights now bounds-checks the heap before peeking (the old code would panic on a type-asserted nil peek of an empty queue).
  • design-a-leaderboard Reset no longer decrements the count for score 0 when the player is absent.

Where Go lacks an ordered-map equivalent (design-a-leaderboard, longest-continuous-subarray-…), a short comment explains the count-map + sorted-slice modelling so the article still reads as a teaching text.

Verification

Every rewritten tab was executed against the production judge via test-problem-article-solutions.ts --lang go, sequentially with a 250 ms delay. 29/29 pass.

Article Rewritten tab(s) Result
binary-tree-diameter Iterative DFS PASS
cheapest-flight-path Dijkstra's Algorithm PASS
depth-of-binary-tree Breadth First Search PASS
design-a-leaderboard Using a TreeMap / SortedMap PASS
design-a-leaderboard Heap for top-K (stray-import fix) PASS
design-twitter-feed Heap; Heap (Optimal) PASS; PASS
find-median-in-a-data-stream Heap PASS
hand-of-straights Heap PASS
invert-a-binary-tree Breadth First Search PASS
k-closest-points-to-origin Min-Heap; Max Heap PASS; PASS
kth-largest-element-in-an-array Min-Heap PASS
kth-largest-integer-in-a-stream Min-Heap PASS
last-stone-weight Heap PASS
longest-continuous-subarray-with-absolute-diff-… Sorted Dict PASS
meeting-schedule-ii Min Heap PASS
merge-k-sorted-linked-lists Heap PASS
min-cost-to-connect-points Prim's Algorithm PASS
minimum-interval-including-query Sweep Line Algorithm; Min Heap PASS; PASS
minimum-stack Brute Force; Two Stacks PASS; PASS
network-delay-time Dijkstra's Algorithm PASS
sliding-window-maximum Heap PASS
swim-in-rising-water Dijkstra's Algorithm PASS
task-scheduling Max-Heap PASS
top-k-elements-in-list Min-Heap PASS
trapping-rain-water Stack PASS
validate-parentheses Stack PASS

The sweep ran every Go tab of each affected problem, not just the rewritten ones. The only failures anywhere were pre-existing Brute Force time-limit-exceeded results on minimum-interval-including-query, sliding-window-maximum, swim-in-rising-water and trapping-rain-water — all untouched by this PR and already failing on main.

Judge config the runs were made against

Run against a neetcode.io working tree carrying in-progress per-language time-limit calibration. Per-problem comparison against neetcode.io main:

  • trapping-rain-water (go: 0.4) and meeting-schedule-ii (go: 0.5) — limits identical to main.
  • task-scheduling and sliding-window-maximum — no go limit on either branch, so both ran on the default 2.5 s CPU allowance, same as main.
  • minimum-interval-including-querygo: 0.45 exists only on the unmerged calibration branch; main has no go entry and would use the 2.5 s default. This tab was therefore verified under a stricter limit than main enforces.
  • All other problems have no timeLimits entry at all and ran on the default, identical to main.

So every result above holds under main's configuration.

Out of scope

Three articles still use godsdata-stream-as-disjoint-intervals, design-a-food-rating-system, seat-reservation-manager. None has a Go judge directory, so their rewrites could not be verified and are deliberately left alone.

Separately, ~44 Go blocks across ~30 other articles still carry import lines and will fail on the judge for the same reason as the design-a-leaderboard tab fixed here. That is a broader cleanup, tracked separately rather than bundled into this PR.

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>
@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 1, 2026

Copy link
Copy Markdown

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

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