leetcode

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const SolutionTask = "judger.judgetask.Judge"

Variables

View Source
var (
	ErrRequestFailed      = errors.New("request failed")
	ErrQuestionNotFound   = errors.New("no matching question found")
	ErrMetadataMissing    = errors.New("question metadata missing")
	ErrNotAuthenticated   = errors.New("not authenticated")
	ErrSubmissionNotFound = errors.New("submission not found")
	ErrUnauthorized       = errors.New("unauthorized: session invalid or expired")
	ErrRateLimited        = errors.New("rate limited: too many requests")
	ErrServerError        = errors.New("leetcode server error")
	ErrInvalidResponse    = errors.New("invalid response format")
)

Functions

This section is empty.

Types

type AuthResponse

type AuthResponse struct {
	Data struct {
		UserStatus UserStatus `json:"userStatus"`
	} `json:"data"`
}

type Client

type Client interface {
	// FetchQuestion fetches a question by its slug.
	FetchQuestion(ctx context.Context, slug string) (*Question, error)

	SubmitTest(ctx context.Context, problem *domain.Problem, snippet string) (string, error)
	SubmitSolution(ctx context.Context, problem *domain.Problem, snippet string) (string, error)
	CheckSubmissionResult(ctx context.Context, url string) (*SubmissionResult, error)

	GetUsername(ctx context.Context) (string, error)
	GetUserStatus(ctx context.Context) (*UserStatus, error)
	IsAuthenticated(ctx context.Context) (bool, error)
	SetSession(session config.Session)
}

type CodeSnippet

type CodeSnippet struct {
	Code     string `json:"code"`
	LangSlug string `json:"langSlug"`
}

type Headers

type Headers map[string]string

type InternalQuestion

type InternalQuestion struct {
	ID           string        `json:"questionFrontendId"`
	SubmitId     string        `json:"questionId"`
	Title        string        `json:"title"`
	TitleSlug    string        `json:"titleSlug"`
	Difficulty   string        `json:"difficulty"`
	Content      string        `json:"content"`
	PaidOnly     bool          `json:"isPaidOnly"`
	CodeSnippets []CodeSnippet `json:"codeSnippets"`
	TestCaseList []string      `json:"exampleTestcaseList"`
	RawMetadata  string        `json:"metadata"`
}

InternalQuestion is used for unmarshaling the raw metadata field

type LeetCodeClient

type LeetCodeClient struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts ...Options) *LeetCodeClient

func WithClient

func WithClient(httpClient *http.Client) *LeetCodeClient

func (*LeetCodeClient) CheckSubmissionResult

func (lc *LeetCodeClient) CheckSubmissionResult(ctx context.Context, submissionId string) (*SubmissionResult, error)

func (*LeetCodeClient) FetchQuestion

func (lc *LeetCodeClient) FetchQuestion(ctx context.Context, slug string) (*Question, error)

func (*LeetCodeClient) GetUserStatus added in v0.0.10

func (lc *LeetCodeClient) GetUserStatus(ctx context.Context) (*UserStatus, error)

func (*LeetCodeClient) GetUsername

func (lc *LeetCodeClient) GetUsername(ctx context.Context) (string, error)

func (*LeetCodeClient) IsAuthenticated

func (lc *LeetCodeClient) IsAuthenticated(ctx context.Context) (bool, error)

func (*LeetCodeClient) SetSession

func (lc *LeetCodeClient) SetSession(session config.Session)

func (*LeetCodeClient) Submit

func (lc *LeetCodeClient) Submit(ctx context.Context, url string, problem *domain.Problem, payload map[string]any) (*SubmitResponse, error)

func (*LeetCodeClient) SubmitSolution

func (lc *LeetCodeClient) SubmitSolution(ctx context.Context, problem *domain.Problem, snippet string) (string, error)

func (*LeetCodeClient) SubmitTest

func (lc *LeetCodeClient) SubmitTest(ctx context.Context, problem *domain.Problem, snippet string) (string, error)

type Options

type Options func(*LeetCodeClient)

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Options

WithHTTPClient sets a custom HTTP client.

func WithSession

func WithSession(session config.Session) Options

type Question

type Question struct {
	ID           string        `json:"questionFrontendId"`
	SubmitId     string        `json:"questionId"`
	Title        string        `json:"title"`
	TitleSlug    string        `json:"titleSlug"`
	Difficulty   string        `json:"difficulty"`
	Content      string        `json:"content"`
	PaidOnly     bool          `json:"isPaidOnly"`
	CodeSnippets []CodeSnippet `json:"codeSnippets"`
	TestCaseList []string      `json:"exampleTestcaseList"`
	Metadata     QuestionMeta  `json:"metadata"`
	LangStatus   map[string]bool
	CreatedAt    string
}

func (*Question) UnmarshalJSON

func (q *Question) UnmarshalJSON(data []byte) error

type QuestionMeta

type QuestionMeta struct {
	Name string `json:"name"`
}

type QuestionReponse

type QuestionReponse struct {
	Data struct {
		Question Question `json:"question"`
	} `json:"data"`
}

type SubmissionID

type SubmissionID string

func (*SubmissionID) UnmarshalJSON

func (s *SubmissionID) UnmarshalJSON(data []byte) error

Custom unmarshal to handle both int and string

type SubmissionResponse

type SubmissionResponse struct {
	SubmissionID       string   `json:"submission_id"`
	QuestionID         string   `json:"question_id"`
	State              string   `json:"state"`      // SUCCESS, FAILED, PENDING
	StatusMsg          string   `json:"status_msg"` // Accepted, Runtime Error, Compile Error
	Correct            bool     `json:"correct_answer"`
	RuntimePercentile  float64  `json:"runtime_percentile"`
	StatusRuntime      string   `json:"status_runtime"`
	MemoryPercentile   float64  `json:"memory_percentile"`
	StatusMemory       string   `json:"status_memory"`
	TotalCorrect       int      `json:"total_correct"`
	TotalTestcases     int      `json:"total_testcases"`
	RuntimeError       string   `json:"runtime_error"`
	FullRuntimeError   string   `json:"full_runtime_error"`
	CompileError       string   `json:"compile_error"`
	FullCompileError   string   `json:"full_compile_error"`
	CodeAnswer         []string `json:"code_answer"`
	ExpectedCodeAnswer []string `json:"expected_code_answer"`
	TaskName           string   `json:"task_name"`
}
	{
	  "status_code": 15,
	  "lang": "javascript",
	  "run_success": false,
	  "runtime_error": "Line 1: SyntaxError: Invalid or unexpected token",
	  "full_runtime_error": "Line 1 in solution.js\nvar romanToInt = function(s) {\\n    const values = {\\n        I: 1,\\n        V: 5,\\n        X: 10,\\n        L: 50,\\n        C: 100,\\n        D: 500,\\n        M: 1000\\n    };\\n\\n    let total = 0;\\n\\n    for (let i = 0; i < s.length; i++) {\\n        const current = values[s[i]];\\n        const next = values[s[i + 1]];\\n\\n        // If current < next, it's a subtractive pair (like IV, IX, etc.)\\n        if (next > current) {\\n            total -= current;\\n        } else {\\n            total += current;\\n        }\\n    }\\n\\n    return total;\\n};\n                              ^\nSyntaxError: Invalid or unexpected token\n    at wrapSafe (node:internal/modules/cjs/loader:1486:18)\n    at Module._compile (node:internal/modules/cjs/loader:1528:20)\n    at Object..js (node:internal/modules/cjs/loader:1706:10)\n    at Module.load (node:internal/modules/cjs/loader:1289:32)\n    at Function._load (node:internal/modules/cjs/loader:1108:12)\n    at TracingChannel.traceSync (node:diagnostics_channel:322:14)\n    at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)\n    at node:internal/main/run_main_module:36:49\nNode.js v22.14.0",
	  "status_runtime": "N/A",
	  "correct_answer": false,
	  "total_correct": 0,
	  "total_testcases": 3,
	 "memory_percentile": null,
	  "pretty_lang": "JavaScript",
	  "submission_id": "runcode_1759702712.9142835_d55B5qU8tt",
	  "status_msg": "Runtime Error",
	  "state": "SUCCESS"
   "compile_error": "Line 6: Char 17: undefined: val (solution.go)",
   "full_compile_error": "Line 6: Char 17: undefined: val (solution.go)",
	}

func (SubmissionResponse) ToResult

func (s SubmissionResponse) ToResult() *SubmissionResult

type SubmissionResult

type SubmissionResult struct {
	State             string
	Answer            bool
	Result            string
	Runtime           string
	RuntimePercentile string
	Memory            string
	MemoryPercentile  string
	ErrorMsg          string
	TestCase          int
	TestOutput        string
	TestExpected      string
	IsSolution        bool
}

func (*SubmissionResult) HasError

func (r *SubmissionResult) HasError() bool

func (*SubmissionResult) IsCorrect

func (r *SubmissionResult) IsCorrect() bool

type SubmitResponse

type SubmitResponse struct {
	InterpretID  string `json:"interpret_id"`
	SubmissionID int64  `json:"submission_id"`
	Testcase     string `json:"test_case"`
}

func (SubmitResponse) GetSubmissionID

func (r SubmitResponse) GetSubmissionID() string

type UserStatus

type UserStatus struct {
	IsSignedIn bool   `json:"isSignedIn"`
	Username   string `json:"username"`
	IsPremium  bool   `json:"isPremium"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL