Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Translate Variables With Pluralization and Nesting

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Translate Variables With Pluralization and Nesting. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Figma plugin interfaces often need localized strings with dynamic values. Implement a translator that renders a template using a nested values object, supporting simple variables, dotted paths, and pluralization blocks.

Use these forms:

  1. Simple variable: {user.name}
  2. Plural block: {count, plural, one{...} other{...}}
  3. Plural branches may contain more variables or nested plural blocks.

For a plural block, render the one branch only when the resolved count equals 1; render other for every other count. Variable values are scalar strings or numbers, and nested objects are accessed with dot-separated paths. Templates are guaranteed to be syntactically valid and contain both one and other branches for every plural block.

Formal specification

Implement translate(template, values), where template is a string and values is a nested dictionary. Return the fully rendered string. A missing path is not included in valid input.

Examples

Example 1

Input: template = "I like {pet.name}.", values = {"pet": {"name": "dogs"}}

Output: "I like dogs."

The dotted path resolves values["pet"]["name"].

Example 2

Input: template = "{count, plural, one{{count} dog} other{{count} dogs}}", values = {"count": 2}

Output: "2 dogs"

The count is not 1, so the other branch is selected.

Constraints

  • 1 <= len(template) <= 10^5
  • Template nesting depth is at most 100
  • Each variable path has at most 20 components
  • Leaf values are strings or integers
  • All templates have valid balanced braces
  • Every plural block contains both one and other branches

Function Signature

def translate(template, values):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output