PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string parsing and template substitution, efficient use of dictionaries for key lookups, and the ability to reason about algorithmic time and space complexity.

  • medium
  • Crowdstrike
  • Coding & Algorithms
  • Software Engineer

Implement templated string replacement

Company: Crowdstrike

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given: 1. A dictionary (map) of placeholder keys to values, for example: ```python values = { "db_host": "example.com", "db_port": 5001 } ``` 2. A template string that may contain placeholders wrapped in double curly braces `{{` and `}}`, for example: ```text "application is now connecting to {{db_host}}:{{db_port}}" ``` ### Task 1 Implement a function that replaces each placeholder in the template string with the corresponding value from the dictionary. For the example above, the output should be: ```text "application is now connecting to example.com:5001" ``` Assume: - All placeholders appearing in the template exist as keys in the dictionary. - Placeholders consist of letters, digits, and underscores only. Describe your approach and then implement the function in a programming language of your choice. ### Task 2 (Follow-up) Now, instead of a single template string, you are given another dictionary where **each value is a template string** that may contain the same kind of `{{key}}` placeholders, and you are also given the same `values` dictionary used for substitution. Example: ```python values = { "db_host": "example.com", "db_port": 5001 } templates = { "conn_msg": "application is now connecting to {{db_host}}:{{db_port}}", "short_msg": "{{db_host}}:{{db_port}}" } ``` Implement a function that takes `values` and `templates` and returns a new dictionary where each value in `templates` has had its placeholders replaced using `values`. Explain your algorithm, including its time complexity in terms of: - `L` = total length of all template strings - `K` = number of distinct keys in the `values` dictionary.

Quick Answer: This question evaluates proficiency in string parsing and template substitution, efficient use of dictionaries for key lookups, and the ability to reason about algorithmic time and space complexity.

Part 1: Replace Placeholders in a Single Template String

You are given a dictionary `values` that maps placeholder keys to values, and a template string that may contain placeholders in the form `{{key}}`. Replace every placeholder with its corresponding value from the dictionary and return the fully rendered string. All placeholders are well-formed, every placeholder key exists in `values`, and keys contain only letters, digits, and underscores. If a value in `values` is not already a string, convert it to text before inserting it.

Constraints

  • 0 <= len(template) <= 100000
  • 0 <= len(values) <= 100000
  • Every placeholder in `template` is well-formed as `{{key}}`
  • Every placeholder key appearing in `template` exists in `values`
  • Keys contain only letters, digits, and underscores

Examples

Input: ({"db_host": "example.com", "db_port": 5001}, "application is now connecting to {{db_host}}:{{db_port}}")

Expected Output: "application is now connecting to example.com:5001"

Explanation: Both placeholders are replaced with their mapped values.

Input: ({}, "plain text only")

Expected Output: "plain text only"

Explanation: Edge case: the template contains no placeholders, so it is returned unchanged.

Input: ({"count": 0}, "{{count}}")

Expected Output: "0"

Explanation: A single placeholder fills the entire string, and the numeric value is converted to text.

Input: ({"a": "X", "b": 7}, "{{a}}{{b}}-{{a}}")

Expected Output: "X7-X"

Explanation: This checks adjacent placeholders and repeated use of the same key.

Input: ({"name": "Ada"}, "")

Expected Output: ""

Explanation: Edge case: an empty template should return an empty string.

Solution

def solution(values, template):
    result = []
    i = 0
    n = len(template)

    while i < n:
        if i + 1 < n and template[i] == '{' and template[i + 1] == '{':
            j = i + 2
            while j + 1 < n and not (template[j] == '}' and template[j + 1] == '}'):
                j += 1
            key = template[i + 2:j]
            result.append(str(values[key]))
            i = j + 2
        else:
            result.append(template[i])
            i += 1

    return ''.join(result)

Time complexity: O(n + r), where n is the length of `template` and r is the total length of inserted replacement text. Space complexity: O(n + r) for the output being built.

Hints

  1. Scan the template from left to right. When you see `{{`, keep moving until you find the matching `}}` to extract the key.
  2. Build the answer using a list of string pieces and join at the end, instead of repeatedly concatenating strings.

Part 2: Replace Placeholders Across a Dictionary of Template Strings

You are given two dictionaries: - `values`: maps placeholder keys to values - `templates`: maps names to template strings Each template string may contain placeholders in the form `{{key}}`. Return a new dictionary with the same keys as `templates`, where every template string has been rendered using `values`. All placeholders are well-formed, every placeholder key exists in `values`, and keys contain only letters, digits, and underscores. Non-string replacement values should be converted to text before insertion.

Constraints

  • 0 <= len(templates) <= 100000
  • Let L be the total length of all strings in `templates`; L can be up to 200000
  • Let K be the number of distinct keys in `values`
  • Every placeholder in every template is well-formed as `{{key}}`
  • Every placeholder key appearing in any template exists in `values`
  • Keys contain only letters, digits, and underscores

Examples

Input: ({"db_host": "example.com", "db_port": 5001}, {"conn_msg": "application is now connecting to {{db_host}}:{{db_port}}", "short_msg": "{{db_host}}:{{db_port}}"})

Expected Output: {"conn_msg": "application is now connecting to example.com:5001", "short_msg": "example.com:5001"}

Explanation: Both template strings are rendered using the same `values` dictionary.

Input: ({"db_host": "example.com"}, {})

Expected Output: {}

Explanation: Edge case: if there are no templates, the result is an empty dictionary.

Input: ({"host": "srv", "port": 8080}, {"empty": "", "literal": "ready", "pair": "{{host}}:{{port}}"})

Expected Output: {"empty": "", "literal": "ready", "pair": "srv:8080"}

Explanation: This checks an empty template string, a template with no placeholders, and a normal replacement.

Input: ({"x": "A", "y": "B"}, {"combo": "{{x}}{{y}}{{x}}", "bracket": "[{{y}}]"})

Expected Output: {"combo": "ABA", "bracket": "[B]"}

Explanation: This checks adjacent placeholders and repeated use of the same key across multiple templates.

Input: ({"host": "local", "port": 0}, {"msg": "{{host}}:{{port}}"})

Expected Output: {"msg": "local:0"}

Explanation: Boundary-style case: a numeric value of 0 should still be converted and inserted correctly.

Solution

def solution(values, templates):
    rendered_values = {key: str(value) for key, value in values.items()}

    def render(template):
        result = []
        i = 0
        n = len(template)

        while i < n:
            if i + 1 < n and template[i] == '{' and template[i + 1] == '{':
                j = i + 2
                while j + 1 < n and not (template[j] == '}' and template[j + 1] == '}'):
                    j += 1
                key = template[i + 2:j]
                result.append(rendered_values[key])
                i = j + 2
            else:
                result.append(template[i])
                i += 1

        return ''.join(result)

    return {name: render(template) for name, template in templates.items()}

Time complexity: O(L + K) average, where L is the total length of all template strings and K is the number of keys in `values`. Space complexity: O(K + L_out), where `L_out` is the total length of the rendered output strings.

Hints

  1. Solve one template string at a time using the same parsing idea as in the single-template version.
  2. If some values are integers, convert each `values[key]` to a string once before processing all templates.
Last updated: Jun 1, 2026

Related Coding Questions

  • Count connected components in a binary grid - Crowdstrike (medium)
  • Maximize the minimum value along a grid path - Crowdstrike (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.