def best_substring(text: str, prefix_string: str, suffix_string: str) -> str:
Given three strings, text, prefixString and suffixString, score the substrings of text:
text matching the end of prefixString.text matching the start of suffixString.Return the substring of text that begins with the matched prefix, ends with the matched suffix, and has the highest total score. If several substrings tie on the total score, return the alphabetically lowest one.
text = "engine"
prefixString = "raven"
suffixString = "ginkgo"
"en" (from engine) matches the end of rav"en" -> prefixScore = 2
"gin" (from engine) matches the start of "gin"kgo -> suffixScore = 3
totalScore = 5, and the answer to return is "engin"
Three reports, February and March 2025, all from Citadel's online assessment: two coding problems, with this string problem second each time. The other problem was counting the distinct palindromic substrings of a string in one report, the best downward path sum in a tree given as an array in another, and a LeetCode problem in the third. One reporter received the assessment immediately after applying online.
One report says no similar problem exists online and that the solution uses dynamic programming; another says it should be solved with KMP. Only one report states the tie rule. None reports input sizes, a function signature or how they did.
Given three strings, text, prefixString and suffixString, score the substrings of text:
text matching the end of prefixString.text matching the start of suffixString.Return the substring of text that begins with the matched prefix, ends with the matched suffix, and has the highest total score. If several substrings tie on the total score, return the alphabetically lowest one.
text = "engine"
prefixString = "raven"
suffixString = "ginkgo"
"en" (from engine) matches the end of rav"en" -> prefixScore = 2
"gin" (from engine) matches the start of "gin"kgo -> suffixScore = 3
totalScore = 5, and the answer to return is "engin"
| Approach | Notes |
|---|---|
| Dynamic programming | One report says the solution is dynamic programming (the report does not show it). Longest-common-substring style tables give each position's match lengths in O(n * (|P| + |S|)). |
| KMP | Another report says it should be solved with KMP. Linear-time matching (KMP's prefix function or the Z-function) gives the same per-position lengths in O(n + |P| + |S|). |
Edge cases probed: Substrings tied on the total score: return the alphabetically lowest (stated in one report's prompt).
Send them this page. It is free to read, no account needed.
What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 62 Citadel questions, refreshed monthly from new candidate reports.