FastPrepParse Nested Message Types

Parse Nested Message Types

Applied Intuition logoApplied Intuition● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Parse a valid message-type expression and return its canonical representation.

A type is an identifier made of letters or digits, optionally followed by angle brackets containing one or more comma-separated child types. ASCII whitespace may appear anywhere outside an identifier. A leaf remains its identifier; a parameterized type is returned as Name(child1,child2,...), recursively preserving child order.

Function

canonicalizeMessageType(expression: String) → String

Examples

Example 1

expression = "Map<String,List<Int>>"return = "Map(String,List(Int))"

The nested angle-bracket grammar becomes a canonical parenthesized tree.

Example 2

expression = " Envelope < Message < String > , Metadata > "return = "Envelope(Message(String),Metadata)"

Whitespace is ignored while child order is preserved.

Constraints

  • 1 <= expression.length <= 10^5
  • The expression is valid under the stated grammar.
  • Nesting depth is at most 500.

More Applied Intuition problems

See Applied Intuition hiring insights
public String canonicalizeMessageType(String expression) {
    // Write your code here.
}
expression"Map<String,List<Int>>"
expected"Map(String,List(Int))"
Checking account…