← Weride Interview Insights

Weride·Software Engineer·Technical Phone Screen·Senior

Senior
Apr 2026

Summary

WeRide software engineer interview with a pretty gnarly string/expression parsing problem. The main challenge was the O(1) space constraint on the base version, which is brutal, and then a follow-up that relaxed it to O(n) but added way more complexity to the input format.

Questions Asked (2)

Q1

Given an expression with lowercase letters, '+', '*', and parentheses where '+' never appears outside any parentheses, return an equivalent expression using only '+' by fully expanding all products (e.g., (a+b)*(c+d) becomes ac+ad+bc+bd). The catch: achieve this with O(1) extra space.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The expansion logic itself isn't too bad once you think of it as distributing terms across parenthesized groups.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the expression is a sum of products of sums, and that expansion can be done by recursively distributing multiplication over addition. Then, propose an in-place algorithm that uses the input string as working space, carefully shifting characters to make room for expanded terms while maintaining O(1) extra space.

Pro tip: Emphasize that O(1) extra space means you cannot use additional data structures proportional to input size; instead, you must manipulate the string in place, possibly using multiple passes and pointer arithmetic. Mention that recursion depth might be a concern, so an iterative approach with explicit stack (but constant size) is preferable.

1. Understand the structure

Recognize that the expression is a sum of products, where each product is a sequence of factors, each factor being either a variable or a parenthesized sum. Since '+' only appears inside parentheses, the top level is a product of sums.

2. Plan expansion strategy

Decide to expand from innermost parentheses outward, or use a recursive descent that builds the expanded form. For O(1) space, consider doing it in-place by shifting characters and overwriting.

3. Design in-place algorithm

Use two pointers: one for reading and one for writing. When expanding a product of sums, compute the Cartesian product of terms and write them sequentially, shifting the remaining unprocessed part of the string to the right as needed.

4. Handle parentheses and operators

Remove parentheses as you expand, and ensure that '+' is inserted between terms. Keep track of the current position and the end of the expanded portion to avoid overwriting unprocessed input.

5. Analyze complexity and edge cases

Argue that the algorithm uses O(1) extra space (only a few pointers and counters) and discuss time complexity, which may be O(n^2) due to shifting. Test with nested parentheses and multiple factors.

Key Points to Mention

  • The expression is a sum of products of sums, so expansion is essentially distributing multiplication over addition.
  • O(1) extra space means no additional arrays or strings proportional to input size; in-place manipulation is required.
  • In-place expansion can be done by shifting characters to the right to make room for new terms, using a write pointer and a read pointer.
  • Recursion may use O(depth) stack space, so an iterative approach with constant extra space is better.
  • Time complexity may be O(n^2) or worse due to repeated shifting, but space is O(1).
  • Edge cases: nested parentheses, multiple factors in a product, and expressions that are already fully expanded.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Follow-up: now '+' can appear outside parentheses too, like in 'a+b*c*(d+e+f)+k+m*(g+h)+i'. Output the fully expanded form. You can use O(n) extra space, such as a stack.

Algorithms & Data StructuresSystem Design
Author's notes

This version is more realistic and honestly more interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to track the current expression string and the operator that precedes each parenthesis. When encountering a '+', push the current expression and reset; when encountering a '(', push the current expression and the operator before it, then start a new expression; when encountering a ')', pop the operator and the previous expression, combine them with the completed inner expression, and continue. Finally, concatenate all parts to get the fully expanded form.

Pro tip: Clarify that the expansion is purely syntactic (removing parentheses and distributing '+' signs) and does not involve multiplication distribution; also mention that the algorithm runs in O(n) time and uses O(n) space, which is optimal for this problem.

1. Understand the problem and constraints

Confirm that the input is a string representing an arithmetic expression with '+', '*', parentheses, and variables. The goal is to output the fully expanded form by removing parentheses and distributing '+' signs, without simplifying multiplication.

2. Design the stack-based algorithm

Use a stack to store the current expression string and the operator that precedes each parenthesis. Iterate through the string, building the current expression; on '+', push the current expression and reset; on '(', push the current expression and the operator before it, then start a new expression; on ')', pop the operator and previous expression, combine them with the inner expression, and continue.

3. Handle edge cases and operator precedence

Ensure that '*' is treated as part of the term and not distributed. Handle cases where parentheses are nested, where '+' appears outside parentheses, and where the expression starts or ends with parentheses.

4. Implement and test with examples

Write code (e.g., in Python) implementing the stack approach. Test with the given example 'a+b*c*(d+e+f)+k+m*(g+h)+i' and other cases like 'a+(b+c)', 'a+(b+(c+d))', and 'a*(b+c)+d' to verify correctness.

5. Analyze complexity and discuss optimizations

State that the algorithm runs in O(n) time and uses O(n) extra space. Discuss potential optimizations like using a linked list or string builder to avoid repeated string concatenation, but note that O(n) space is acceptable per the problem.

Key Points to Mention

  • Stack usage to track expressions and operators before parentheses
  • Distinction between '+' distribution and '*' non-distribution
  • Handling of nested parentheses and multiple terms
  • Time and space complexity: O(n) time, O(n) space
  • Edge cases: empty parentheses, leading/trailing operators, multiple '+' signs
  • Implementation details: string building, avoiding unnecessary copying

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.