Skip to main content
All Questions

Replace '?' characters to minimize a string's total penalty, returning the lexicographically smallest result.

Unlock detailed company stats for this questionUpgrade

There are 26 categories and each data entry is labeled with one, entries[i] being a lowercase English letter; some entries[i] are '?', meaning unknown and must be replaced with a lowercase letter. The penalty of index i is the number of indices before i that have the same character as entries[i]. Examples: "hello" -> [0,0,0,1,0]; "abc" -> [0,0,0]; "aaccbbc" -> [0,1,0,1,0,1,2]. Task: replace all '?' so the total penalty is minimum; if multiple strings achieve the minimum, return the lexicographically smallest. Function: string calculateOptimalEntries(string entries). Constraints: 1 <= |entries| <= 10^5; at least one '?'; all characters lowercase letters or '?'. Sample: entries = "abcd?" -> "abcde" (zero penalty, lexicographically smallest).

Format · Amazon OA.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.