PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates parsing of algebraic expressions, application of the distributive law, string manipulation, and algorithmic reasoning about time and extra-space complexity.

  • Medium
  • WeRide
  • Coding & Algorithms
  • Software Engineer

Expand algebraic expression with distribution

Company: WeRide

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Question Given an expression of lowercase letters, '+', '*', and parentheses with no '+' outside any parentheses, return an equivalent expression containing only '+' by fully expanding all products (e.g., (a+b)*(c+d) → ac+ad+bc+bd). Achieve O( 1) extra-space complexity. Follow-up: If '+' may appear outside parentheses (e.g., a+b*c*(d+e+f)+k+m*(g+h)+i), output the fully expanded '+'-only form (a+bcd+bce+bcf+k+mg+mh+i). Provide working code that may use O(n) extra space (e.g., a stack).

Quick Answer: This question evaluates parsing of algebraic expressions, application of the distributive law, string manipulation, and algorithmic reasoning about time and extra-space complexity.

You are given a string `expr` representing an algebraic expression made of lowercase letters, the operators `+` (addition) and `*` (multiplication), and parentheses. Multiplication binds tighter than addition, and parentheses group sub-expressions. Each letter behaves like a variable, and concatenation denotes a product (so `a*b` is written `ab` after expansion). Return an equivalent expression that uses ONLY `+`, obtained by fully distributing every product over every sum (i.e., expand into a flat sum of monomials). Within each resulting monomial, keep the letters in the left-to-right order in which they were multiplied (do NOT sort letters). Keep the monomials in the natural left-to-right traversal order produced by distribution, and join them with `+`. For example: - `(a+b)*(c+d)` expands to `ac+ad+bc+bd`. - `a+b*c*(d+e+f)+k+m*(g+h)+i` expands to `a+bcd+bce+bcf+k+mg+mh+i`. The input is always a well-formed expression (balanced parentheses, no unary operators, no empty groups). The original problem first restricts `+` to appear only inside parentheses and asks for O(1) extra space; the general follow-up allows `+` anywhere (outside parentheses too). This solution handles the general case, which subsumes the restricted one, using a stack-based recursive parse (O(n) auxiliary space for the parse stack and the output list of monomials).

Constraints

  • 1 <= len(expr)
  • expr contains only lowercase letters 'a'-'z', '+', '*', '(' and ')'
  • Parentheses are balanced and there are no empty groups
  • Multiplication has higher precedence than addition; operators are binary
  • Within a monomial, letters appear in multiplication (left-to-right) order and are NOT re-sorted

Examples

Input: ("(a+b)*(c+d)",)

Expected Output: "ac+ad+bc+bd"

Explanation: Distribute the product of two binomials: each term of the first sum times each term of the second.

Input: ("a+b*c*(d+e+f)+k+m*(g+h)+i",)

Expected Output: "a+bcd+bce+bcf+k+mg+mh+i"

Explanation: Follow-up case with '+' outside parentheses: 'a', 'k', 'i' pass through; b*c*(d+e+f) expands to bcd+bce+bcf; m*(g+h) expands to mg+mh.

Input: ("a",)

Expected Output: "a"

Explanation: Edge case: a single letter is already fully expanded.

Input: ("a*b",)

Expected Output: "ab"

Explanation: A single product of two letters becomes the concatenated monomial 'ab'.

Input: ("(a+b)*(c+d)*(e+f)",)

Expected Output: "ace+acf+ade+adf+bce+bcf+bde+bdf"

Explanation: Three binomials multiplied: 2*2*2 = 8 monomials, each a choice of one letter from each factor in order.

Input: ("(a+b+c)*d",)

Expected Output: "ad+bd+cd"

Explanation: Distribute a trinomial across a single letter on the right.

Input: ("x*(y+z)",)

Expected Output: "xy+xz"

Explanation: Single letter times a binomial distributes to two monomials.

Hints

  1. Model the expression with a recursive-descent grammar: expr = term ('+' term)*, term = factor ('*' factor)*, factor = letter | '(' expr ')'.
  2. Have each parse function return a LIST of monomials (strings). A sum concatenates the two lists; a product takes the cartesian product, concatenating each left monomial with each right monomial in order.
  3. A single letter parses to a one-element list [letter]. Joining the final list with '+' gives the answer. The recursion stack provides the O(n) extra space the follow-up permits.
Last updated: Jun 25, 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

  • Implement Several Core Algorithmic Components - WeRide (medium)
  • Validate Bracket Sequence - WeRide (medium)
  • Implement matrix multiplication and fast exponentiation - WeRide (medium)
  • Implement expression expansion to plus-only form - WeRide (Medium)