PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string manipulation, pattern matching, and interval-merging competency within algorithmic problem-solving for text processing and HTML-like formatting.

  • medium
  • Apple
  • Coding & Algorithms
  • Machine Learning Engineer

Wrap Matching Substrings in Bold Tags

Company: Apple

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

During a machine learning engineer phone screen, the coding task was: You are given a string `text` and a list of strings `patterns`. Return a new string where every substring of `text` that matches any string in `patterns` is wrapped in HTML-style bold tags: `<b>` and `</b>`. Rules: - If two matched ranges overlap, they must be merged into one bold segment. - If two matched ranges are adjacent, they should also be merged into one bold segment. - The output should contain the minimum number of bold tag pairs needed to cover all matches. Example 1: - Input: `text = "abcxyz123"`, `patterns = ["abc", "123"]` - Output: `"<b>abc</b>xyz<b>123</b>"` Example 2: - Input: `text = "aaabbcc"`, `patterns = ["aaa", "aab", "bc"]` - Output: `"<b>aaabbc</b>c"` Implement a function that returns the formatted string.

Quick Answer: This question evaluates string manipulation, pattern matching, and interval-merging competency within algorithmic problem-solving for text processing and HTML-like formatting.

You are given a string `text` and a list of strings `patterns`. Return a new string where every occurrence of any pattern in `text` is wrapped in HTML-style bold tags: `<b>` and `</b>`. If two matched ranges overlap, they must be merged into one bold segment. If two matched ranges are adjacent, they should also be merged. The final output must use the minimum number of bold tag pairs needed to cover all matches.

Constraints

  • 0 <= len(text) <= 1000
  • 0 <= len(patterns) <= 100
  • 1 <= len(patterns[i]) <= 100 for each pattern that exists
  • Both `text` and `patterns[i]` consist of lowercase English letters

Examples

Input: ("abcxyz123", ["abc", "123"])

Expected Output: "<b>abc</b>xyz<b>123</b>"

Explanation: The matches are `abc` and `123`, and they are separate, so they become two bold segments.

Input: ("aaabbcc", ["aaa", "aab", "bc"])

Expected Output: "<b>aaabbc</b>c"

Explanation: `aaa` and `aab` overlap near the start, and `bc` touches that merged region, so all matched characters from index 0 to 5 are wrapped together.

Input: ("ababcd", ["ab", "abc", "bcd"])

Expected Output: "<b>ababcd</b>"

Explanation: The matches overlap and connect across the whole string, so a single pair of tags wraps everything.

Input: ("hello", [])

Expected Output: "hello"

Explanation: With no patterns, nothing is bolded.

Input: ("", ["a", "bc"])

Expected Output: ""

Explanation: An empty text produces an empty result.

Input: ("a", ["a", "aa"])

Expected Output: "<b>a</b>"

Explanation: The single character matches `a`, so the whole string is bolded.

Input: ("abcdef", ["gh", "ij"])

Expected Output: "abcdef"

Explanation: None of the patterns appear in the text, so the output is unchanged.

Hints

  1. Try marking which character positions belong to at least one matched pattern, then build the answer from those marks.
  2. While scanning from left to right, keep track of the furthest end index of any pattern that starts at or before the current position.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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.

Related Coding Questions

  • Minimum Cells to Bridge a Magic Grid - Apple (hard)
  • Find Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)