Generate all strings by expanding wildcards

Quick Overview

This question evaluates algorithmic string manipulation and combinatorial enumeration skills, focusing on handling wildcard characters and generating all possible binary outcomes from a pattern.

Generate all strings by expanding wildcards

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given a string `s` consisting only of characters `'0'`, `'1'`, and `'*'`, replace every `'*'` with either `'0'` or `'1'` and return **all possible resulting binary strings**. You may return the results in **any order**. ## Example Input: `"01*"` Output: `["010", "011"]` Input: `"1*0*"` Output: `["1000","1001","1100","1101"]`

Overview: This question evaluates algorithmic string manipulation and combinatorial enumeration skills, focusing on handling wildcard characters and generating all possible binary outcomes from a pattern.

Community answers

Answer by bansalmohit.217

For every character in the string: If it's 0 or 1 → keep it and move to the next character. If it's → we have 2 choices**: Replace it with 0 Replace it with 1 Once we've processed the entire string, add the constructed string to the result. Think of every as a decision point**. function generateBinaryStrings(s) { const results = []; function backtrack(index, current) { if(index === s.length){ results.push(current); return; } if(s[index] === '0' || s[index] === '1'){ backtrack(index+1, current + s[index]) } else{ backtrack(index+1, current + '0') backtrack(index+1, current + '1') } } backtrack(0, "") return results; }
|Home/Coding & Algorithms/Bloomberg
Bloomberg logo
Bloomberg
Dec 15, 2025
mediumSoftware EngineerOnsiteCoding & Algorithms
21
0

Problem

Given a string s consisting only of characters '0', '1', and '*', replace every '*' with either '0' or '1' and return all possible resulting binary strings.

You may return the results in any order.

Example

Input: "01*"

Output: ["010", "011"]

Input: "1*0*"

Output: ["1000","1001","1100","1101"]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...