Shared Approver Lookup accepts a copied ancestor

オープン
#842 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@weichieh0204 がすでに取り組んでいます。

2026年11月25日 から。

  • #846 @weichieh0204 による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
c, cpp, java, javascript, python
領域
testing

調査の方向性

Start with the judge spec in web/judges/shared-approver-lookup.json and the identity contract in problem-bank/variants.json, then see how generateHarness in web/compiler-explorer.js serializes returned trees and how checkAnswer in web/lib.js compares them. Check linked PR #846 first, since it may already cover this. Done means a copied ancestor fails with a per-case error, the original node still passes 6/6, and tree-building exercises that allow new nodes are unaffected.

索引モデルが issue の本文から書いたものです。

説明

Observed behavior

Shared Approver Lookup (lowest-common-ancestor-of-a-binary-tree) accepts a newly allocated copy of the correct ancestor's subtree. The copied answer passes all six built-in cases in C, C++, Java, Python and JavaScript, even though the scenario requires the existing input node.

Expected behavior

A copied ancestor should fail with a per-case error. Returning the original ancestor should still pass. The contract in problem-bank/variants.json explicitly states: "the original node must be returned, not a copy."

Reproduction
  1. Use a source checkout at revision 7023a392dfbd26656c73098b90dad43ec3b751b6.
  2. Load the judge specification from web/judges/shared-approver-lookup.json.
  3. Use generateHarness("c", spec, candidateCode) from web/compiler-explorer.js to generate solution.c with the C candidate solution below. The harness supplies struct TreeNode.
  4. Compile with gcc -std=c17 solution.c -o solution and run the generated program.
  5. Parse its output with parseCompilerResults and grade each result against the judge's cases using checkAnswer from web/lib.js. Observe that all six cases pass, although the solution returns newly allocated copies.
  6. Replace return copySubtree(findAncestor(root, p, q)); with return findAncestor(root, p, q);, regenerate the harness, and rerun. This version also passes 6/6, but returns the required original node.

The incorrect C candidate solution used in step 3:

#include <stdlib.h>

static struct TreeNode* findAncestor(
    struct TreeNode* root,
    struct TreeNode* p,
    struct TreeNode* q
) {
    if (!root || root == p || root == q)
        return root;

    struct TreeNode* left = findAncestor(root->left, p, q);
    struct TreeNode* right = findAncestor(root->right, p, q);
    return left && right ? root : left ? left : right;
}

static struct TreeNode* copySubtree(struct TreeNode* root) {
    if (!root)
        return NULL;

    struct TreeNode* copy = malloc(sizeof(*copy));
    if (!copy)
        exit(EXIT_FAILURE);

    copy->val = root->val;
    copy->left = copySubtree(root->left);
    copy->right = copySubtree(root->right);
    return copy;
}

struct TreeNode* nearestSharedApprover(
    struct TreeNode* root,
    struct TreeNode* p,
    struct TreeNode* q
) {
    return copySubtree(findAncestor(root, p, q));
}
CodeTrial version

codetrial 0.1.0 (7023a39)

Environment

OS: Windows x86_64
Browser: Not used; runner-level reproduction
Language: C, C++, Java, Python and JavaScript
Interview loop: Not applicable
Problem: Shared Approver Lookup (lowest-common-ancestor-of-a-binary-tree)
REACTO or STAR step: Not applicable
Reconnect: Not applicable

Node.js v24.19.0; MinGW GCC/G++ 15.2.0; CPython 3.13.5.

C and C++ were compiled from the generated harnesses. Python used the exported driver, JavaScript used the actual worker source in a Node VM, and Java used the generated harness on Compiler Explorer java2501.

Sanitized evidence

The copied-ancestor solution passed all six built-in cases in all five languages.

Language Copied ancestor
C 6/6 passed
C++ 6/6 passed
Java 6/6 passed
Python 6/6 passed
JavaScript 6/6 passed

Serialization preserves values and shape but loses node identity.

I plan to capture the input node identities before candidate execution and validate the returned node before serialization for binary-tree outputs with treeNodeValue arguments. Tree-building exercises that allow new nodes should remain unaffected.

A JavaScript prototype keeps the original-node solution at 6/6, rejects the copied solution in all six cases, and accepts an allocation-allowed tree control. Please assign this issue to me.

Public data confirmation
  • I removed credentials, session tokens, candidate recordings, and personal interview content from this public report.
主要言語
Rust
スター
236
フォーク
153
平均マージ
1日 9時間
マージ済み PR(30日)
187

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sysprog21/codetrial のほかの issue

sysprog21/codetrial の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。