Rank Word-Translation Hints from Sentence Translations
Updated
Problem statement
An English sentence has one list of Spanish translation hints for each whitespace-delimited word occurrence. You are also given one bestTranslation and ordered alternativeTranslations. Every translation has the same number of whitespace-delimited tokens as the English sentence.
For each word position, rank its hints in two stages:
- Lexicographically sort the hints.
- Assign sentence priority. A hint matching the normalized token at that position in
bestTranslationhas priority0. Otherwise, its priority isj + 1for the first alternative translation at indexjwhose token matches. A hint appearing in no sentence has priorityalternativeTranslations.length + 1. Sort by priority, using the stage-one lexicographic order to break ties.
Normalization lowercases a token and removes all leading and trailing ASCII punctuation characters .,!?;:. Punctuation inside a token is retained. Return the sorted hint list for every word position, preserving the original hint strings in the output.
Function
rankTranslationHints(sentence: String, hints: String[][], bestTranslation: String, alternativeTranslations: String[]) → String[][]Examples
Example 1
sentence = "I eat apples."hints = [["me","yo"],["comer","como"],["fruta","manzana","manzanas"]]bestTranslation = "Yo como manzanas."alternativeTranslations = ["Me comer fruta.","Yo comer manzana!"]return = [["yo","me"],["como","comer"],["manzanas","fruta","manzana"]]Best-sentence tokens come first. For the final word, fruta appears in the first alternative and manzana in the second. Trailing punctuation is removed for matching.
Example 2
sentence = "Hello world"hints = [["hola","buenas"],["tierra","mundo"]]bestTranslation = "¡Hola mundo!"alternativeTranslations = []return = [["buenas","hola"],["mundo","tierra"]]The contract strips only the listed ASCII punctuation. The leading inverted exclamation mark in ¡Hola is retained, so neither first-position hint matches and lexical order decides that list. The trailing ASCII ! is stripped from mundo!.
Constraints
- The English sentence contains between
1and100whitespace-delimited tokens. hints.lengthequals the English token count.- Each hint list contains between
1and50non-empty strings. bestTranslationand every alternative contain the same number of whitespace-delimited tokens as the English sentence.0 <= alternativeTranslations.length <= 20.- Lexicographic comparison uses the original string values.
More Duolingo interview questions
- Longest Decreasing Matrix Path with Limited RelaxationsONSITE INTERVIEW · Seen Nov 2025
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
- Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
- Sum All Values in a Binary TreePHONE SCREEN · Seen Oct 2026
- User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
- Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026
Similar questions at other companies
- Parse and Order Structured LogsCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEWGoogle interview questions