FastPrepRank Word-Translation Hints from Sentence Translations

Rank Word-Translation Hints from Sentence Translations

Duolingo logoDuolingo● MediumNEW GRADONSITE INTERVIEW
Learn

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:

  1. Lexicographically sort the hints.
  2. Assign sentence priority. A hint matching the normalized token at that position in bestTranslation has priority 0. Otherwise, its priority is j + 1 for the first alternative translation at index j whose token matches. A hint appearing in no sentence has priority alternativeTranslations.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 1 and 100 whitespace-delimited tokens.
  • hints.length equals the English token count.
  • Each hint list contains between 1 and 50 non-empty strings.
  • bestTranslation and 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 problems

See Duolingo hiring insights
public String[][] rankTranslationHints(String sentence, String[][] hints, String bestTranslation, String[] alternativeTranslations) {
    // write your code here
}
sentence"I eat apples."
hints[["me","yo"],["comer","como"],["fruta","manzana","manzanas"]]
bestTranslation"Yo como manzanas."
alternativeTranslations["Me comer fruta.","Yo comer manzana!"]
expected[["yo", "me"], ["como", "comer"], ["manzanas", "fruta", "manzana"]]
Checking account…