Order Per-Word Translation Candidates in a Nested Sentence Data Model

Quick Overview

A pair-programming exercise in an existing Python codebase where a sentence, its words and each word's Spanish translation candidates sit inside deeply nested data classes loaded from JSON. It asks for an alphabetical ordering per word, then an ordering driven by interviewer-supplied rules, and tests code reading, clarifying a spec and testing.

Order Per-Word Translation Candidates in a Nested Sentence Data Model

Company: Duolingo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

You are pairing with an interviewer for 75 minutes inside an existing Python codebase. The codebase loads one large JSON document into many nested data classes. Somewhere in that structure is a sentence: its words in order, and for each word a list of candidate translations into Spanish. For example, the English sentence "They are boys" might carry these candidates (the letters stand in for Spanish words): | Word | Candidate translations | |---|---| | They | `[x, y]` | | are | `[z]` | | boys | `[a, b, c, d]` | You will add functions that order each word's candidates, talking through your reasoning as you go. Expect a large share of the session to go into reading the nested model and finding the fields you need. ### Clarifying Questions - Which class owns the sentence, and is word order stored in a field (such as an index) or implied by list position? - Should the functions return new structures, or reorder the candidate lists in place even though other modules may hold references to the same objects? - Can a word have no candidates, or the same candidate twice? - How should capital letters and accented characters (á, é, ñ) compare? ### Part 1: Alphabetical order for each word Write a function that takes the sentence object and returns, for each word in sentence order, its candidate translations sorted alphabetically. Words keep their positions; only each word's candidate list is ordered. In the example, `boys` maps to `[a, b, c, d]` and `They` maps to `[x, y]`. ```hint Find the path first Before writing any sorting code, write a small accessor that walks from the sentence object down to one word's candidate list, and check it on a single word. ``` #### What This Part Should Cover - Reaching each word's candidates through the nested classes in one place, so traversal logic is not repeated - A definition of "alphabetical" that suits Spanish text (case, accents, ñ) - Preserving sentence order and stating whether the loaded model is modified ### Part 2: Ordering by the supplied rules Next, the interviewer introduces ordering rules for each word's translations and asks you to order the candidates by those rules. The rules come with the exercise and are considerably more complex than alphabetical order. Before coding, get them stated precisely: what each rule compares, where the inputs to each rule live in the JSON, and what decides a tie. Then implement the rule-based ordering so it reuses your traversal from Part 1 and can be tested rule by rule. ```hint Work one word by hand Ask the interviewer to confirm the expected output for one word with several candidates before you write the code that produces it. ``` #### Clarifying Questions for this Part - Do the rules differ from word to word, or does one rule list apply to the whole sentence? - When two candidates tie under every rule, does alphabetical order break the tie, or the original order? - What should happen when a candidate lacks a field that a rule needs? #### What This Part Should Cover - Turning a loosely described rule set into a testable statement before implementing it - Representing the rules so that a new rule can be added without rewriting the sort - Deterministic tie-breaking and defined behavior for missing fields ### What a Strong Answer Covers - Reading unfamiliar code efficiently: tracing one path through the model, building a small fixture, and confirming assumptions out loud - Keeping traversal of the nested model separate from the ordering logic - Tests for each ordering, including empty candidate lists, single candidates, ties and accented words - Raising confusion about the specification early and proposing a confirmed smaller step before the session runs out ### Follow-up Questions - How would you check that the loaded JSON matches the structure your code assumes, and what error would you raise when it does not? - If the codebase held thousands of sentences, where would you precompute or cache sort keys? - How would your design change if ordering rules had to be configurable per language pair?

Overview: A pair-programming exercise in an existing Python codebase where a sentence, its words and each word's Spanish translation candidates sit inside deeply nested data classes loaded from JSON. It asks for an alphabetical ordering per word, then an ordering driven by interviewer-supplied rules, and tests code reading, clarifying a spec and testing.

Read the full Duolingo Software Engineer interview experience this question came from

Duolingo logo
Duolingo
Nov 12, 2026
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

You are pairing with an interviewer for 75 minutes inside an existing Python codebase. The codebase loads one large JSON document into many nested data classes. Somewhere in that structure is a sentence: its words in order, and for each word a list of candidate translations into Spanish.

For example, the English sentence "They are boys" might carry these candidates (the letters stand in for Spanish words):

WordCandidate translations
They[x, y]
are[z]
boys[a, b, c, d]

You will add functions that order each word's candidates, talking through your reasoning as you go. Expect a large share of the session to go into reading the nested model and finding the fields you need.

Clarifying Questions Guidance

  • Which class owns the sentence, and is word order stored in a field (such as an index) or implied by list position?
  • Should the functions return new structures, or reorder the candidate lists in place even though other modules may hold references to the same objects?
  • Can a word have no candidates, or the same candidate twice?
  • How should capital letters and accented characters (á, é, ñ) compare?

Part 1: Alphabetical order for each word

Write a function that takes the sentence object and returns, for each word in sentence order, its candidate translations sorted alphabetically. Words keep their positions; only each word's candidate list is ordered. In the example, boys maps to [a, b, c, d] and They maps to [x, y].

What This Part Should Cover Guidance

  • Reaching each word's candidates through the nested classes in one place, so traversal logic is not repeated
  • A definition of "alphabetical" that suits Spanish text (case, accents, ñ)
  • Preserving sentence order and stating whether the loaded model is modified

Part 2: Ordering by the supplied rules

Next, the interviewer introduces ordering rules for each word's translations and asks you to order the candidates by those rules. The rules come with the exercise and are considerably more complex than alphabetical order. Before coding, get them stated precisely: what each rule compares, where the inputs to each rule live in the JSON, and what decides a tie. Then implement the rule-based ordering so it reuses your traversal from Part 1 and can be tested rule by rule.

Clarifying Questions for this Part Guidance

  • Do the rules differ from word to word, or does one rule list apply to the whole sentence?
  • When two candidates tie under every rule, does alphabetical order break the tie, or the original order?
  • What should happen when a candidate lacks a field that a rule needs?

What This Part Should Cover Guidance

  • Turning a loosely described rule set into a testable statement before implementing it
  • Representing the rules so that a new rule can be added without rewriting the sort
  • Deterministic tie-breaking and defined behavior for missing fields

What a Strong Answer Covers Guidance

  • Reading unfamiliar code efficiently: tracing one path through the model, building a small fixture, and confirming assumptions out loud
  • Keeping traversal of the nested model separate from the ordering logic
  • Tests for each ordering, including empty candidate lists, single candidates, ties and accented words
  • Raising confusion about the specification early and proposing a confirmed smaller step before the session runs out

Follow-up Questions Guidance

  • How would you check that the loaded JSON matches the structure your code assumes, and what error would you raise when it does not?
  • If the codebase held thousands of sentences, where would you precompute or cache sort keys?
  • How would your design change if ordering rules had to be configurable per language pair?