Render Agent Response Templates With Nested Variable Lookups
Last updated: November 29, 2026
Quick Overview
Implement render(template, ctx) that replaces placeholders like {{customer.name}} or {{orders.1.id}} with values from a nested dict and list context. Raise a clear error for missing paths and for unclosed or empty placeholders.
Sierra
November 29, 20260
0
0 solved
Implement render(template, ctx) that replaces placeholders like {{customer.name}} or {{orders.1.id}} with values from a nested dict and list context. Raise a clear error for missing paths and for unclosed or empty placeholders.
Sierra agents often fill structured messages, such as order confirmations, with data fetched from a customer's systems, and a silent blank in a customer-facing message is a real bug. The interviewer looks for a clean single-pass parser, careful index handling, and errors that fail loudly.
What the Interviewer Expects
- Scan the string once using find instead of regex backtracking
- Support dotted paths through dicts and numeric indices through lists
- Raise on missing keys instead of rendering an empty string
- Reject unclosed and empty placeholders
- Keep literal text outside placeholders untouched
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support a default value such as {{customer.nickname | 'there'}}?
- How would you escape a literal {{ in output?
- How would you prevent a template from reading fields the agent should not expose, such as a full card number?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Placeholders are {{ path }} with optional surrounding spaces. A path is dot-separated: dict keys, or non-negative integers when the current value is a list. Missing data raises KeyError(path). A `...
High-Level Design
Walk the template with an index i. Find the next {{; copy literal text before it; find the matching }}; resolve the path; append the value; continue after }}. Collect pieces in a list and join...