Fail mirrored runs copied from the reversed read

HA
28m ago
+411-1

Problem and behavior

In Mirrored Tag Segment (longest-palindromic-substring, LeetCode 5), several wrong scans passed all five judge cases, because every read happened to give each of them a longest mirrored stretch. The one the issue reports returns the longest common substring of the read and its reversed copy without checking that the result is itself mirrored, the approach the LeetCode editorial for problem 5 warns about. The others expand odd centres but keep an adjacent equal pair as it stands, skip the centres at the ends of the read or stop the even centres one short of the last pair, compare characters case-folded or drop everything but letters, or everything but lowercase letters and digits, although the contract promises exact matching of digits and letters, keep the first or the last stretch that grows past one character, or settle one kind of centre, odd or even, before trying the other. The judge is what the interviewer and the report trust, so a full green run credited solutions a human interviewer would reject. Each of these now fails a case named for its read, and a correct solution passes all fifteen.

Wrong scanCase that rejects itIt returns
longest common substring with the reversed copy, either tie pickmirrored ends: s="abacdfgdcaba"abacd or dcaba
adjacent equal pair never expandedwhole string even: s="abba"bb
centres that skip the endsmatching prefix: s="aab"a
even centres that stop before the last pairmatching suffix: s="baa"b
case-folded comparisonmixed case: s="aA"aA
everything but letters droppeddigit at the centre: s="ab1ba"abba
everything but lowercase letters and digits droppedcapital at the centre: s="abBba"abba
first or last stretch past one charactermiddle mirror: s="abacdedcfgf"aba or fgf
odd centres settled before even oneseven mirror beats odd mirror: s="abacca"aba
even centres settled before odd onesodd mirror beats even mirror: s="aabcb"aa

Approach

Each added read gives one family a wrong answer, so each case is needed, and none depends on which tied stretch an implementation keeps. The browser regression in tests/browser/compiler-explorer.test.js writes centre expansion once and states each scan built on it as the one thing it changes (the centres it visits, how two characters compare, which stretch it keeps, which kind of centre it tries first), so the copies cannot drift apart; the reversed-copy shortcut and the unexpanded pair stay loops of their own, since neither is centre expansion with one option changed. Every scan runs by name against the case pinned for it, with the exact string it returns and the palindrome checker's refusal asserted, and centre expansion runs through the checker on every case. The palindrome checker reads only the length of a case's expected, so a wrong recorded answer could pass every run and be shown as the answer; tests/browser/judge-case-counts.test.js, beside the other bank-wide judge invariant, now runs every judge case's expected through its own checker, which proves membership while the shortcut test pins the length. Labels describe the read, not the shortcut, and pass rules.check_labels; tests/web/contract.rs pins all ten; web/judges/mirrored-tag-segment.json and the digest in tests/golden/task-posed.json are regenerated, not hand-edited. The page example names case 4, so it still shows the same read. The cases arrive in five commits and the bank-wide check in a sixth, each under the interactive-review threshold.

Validation

The branch sits on current main (9817ef6a). ./scripts/test.sh ran to the end twice on this head. The second run passed every gate; its browser lane passed 3809 of 3843 with 34 skipped for javac and java, and it ended:

not checked by this run:
  desktop clippy and tests: libwebkit2gtk-4.1-dev builds desktop/
  python tests, test_the_tool_finds_an_interpreter_that_can_build (test_task_authoring.TaskAuthoringTests.test_the_tool_finds_an_interpreter_that_can_build): the tests run outside a venv
  browser tests, 14 skipped: javac is not available
  browser tests, 20 skipped: javac/java are not available

The first run failed one browser test, explicit Start mixes tab audio and the existing mic into a downloadable WebM in tests/browser/local-recorder.test.js, where the 440 Hz level of the recording came out at 0.0065 against a floor of 0.01. It passed 6 runs of 6 on its own and 12 of 12 run four at a time, both on this head and on 2612c5f3, and this change touches no recording code, so I left it out of this pull request. Not checked here: desktop clippy and tests, and the Java harness runs.

Beyond the scans the test names, 29 single-change wrong variants of centre expansion, the DP table and brute force (centre ranges and loop bounds, slice ends, character filters, comparisons, which stretch is kept, centre order) were run against the fifteen cases, and each fails at least one; the variants that still pass are equivalent to a correct scan, such as skipping the odd centres at the ends, which only ever yield one character. Dropping either new case, or making its read lowercase, fails the suite by the case's name, and the centre-visiting predicate gives the same answer as the scan it replaced on every read over abA1 up to six characters and on 30,000 random reads over abAB1 up to nine.

Close #850


Summary by cubic

Adds ten longest-palindromic-substring judge cases so wrong scans that previously passed every existing case now fail. The judge gave a full green run to solutions an interviewer would reject: returning the longest common substring of a read and its reversed copy without checking the result is mirrored, keeping an adjacent equal pair instead of expanding it, skipping the centres at the read's ends, comparing characters case-folded, dropping everything but letters or dropping capitals too, stopping even centres one short of the last pair, keeping the first or last stretch that grows past one character, and settling odd or even centres before trying the other. Each scan now fails a case named for its read, and a correct centre-expansion solution passes all fifteen cases.

Regression coverage

  • Runs each wrong scan by name against its pinned case, asserting the exact string it returns and that the checker refuses it, plus a centre-expansion reference against every case.
  • Runs every judge case's expected value through its own checker, so a wrong recorded answer can no longer slip into any judge.
  • Pins the ten new labels in tests/web/contract.rs; the regenerated judge file and golden digest follow automatically. Closes #850.