Implement menu parser and serializer
Menu Parser and Serializer
You are tasked with implementing logic to convert a restaurant menu between a plain text format and a tree data structure. The text format uses indentation to represent the hierarchy of categories and items.
Text Format
- The menu is provided as a multi-line string, where each non-empty line describes a single menu item.
- Leading indentation, consisting of zero or more groups of two spaces, indicates the nesting depth relative to top-level categories.
- Following the indentation, fields on each line are separated by the
|character in the format:<type>|<id>|<name>|<price>.type: A string label (e.g.,CATEGORY,FOOD,SIDE). Do not assume only these three types; treat it as an arbitrary string.id: A unique identifier string for the item.name: Free text that is guaranteed not to contain the|character.price: A decimal number (e.g.,5.99), or the literal-if the item does not have a price (e.g., a category).
Example Input:
CATEGORY|c1|Burgers|-
FOOD|f1|Classic Burger|5.99
SIDE|s1|Fries|1.99
FOOD|f2|Veggie Burger|6.49
CATEGORY|c2|Drinks|-
FOOD|f3|Cola|1.50
FOOD|f4|Water|1.00
This example demonstrates a hierarchy like category > food > side, but the general depth and item types can vary.
Tree Structure
Define a language-agnostic data structure, MenuItem, with at least the following fields:
type: stringid: stringname: stringprice: nullable/optional number (should benullif the text price is-)children: An ordered list or array ofMenuItemobjects, representing nested items.
Top-level menu items are those with zero indentation in the text format.
Tasks
parseMenu(text): Implement a function that accepts the menu text string and returns the corresponding tree. The output should be a list/array of top-levelMenuItemnodes, with allchildrenfields properly populated.serializeMenu(items): Implement a function that accepts the tree (the list/array of top-levelMenuItemnodes) and returns a canonical text representation. This output must exactly match the format described above.
Serializer Requirements:
- Use exactly two spaces for each level of indentation deeper than a parent.
- Use
|as the field separator. - Output
-for items whosepriceisnullor absent. - Output a depth-first traversal, ensuring all children of a node appear immediately after their parent, in the order they are stored in the
childrenlist.
Assumptions and Constraints:
- You may assume the input text is always a valid, well-formed tree (i.e., no cycles, consistent indentation, and at least one top-level item).
- Design your parsing and serialization logic to run in O(N) time and O(N) extra space, where N is the number of menu items (lines) in the input.
AI interview prep
Core dilemma
The central engineering conflict is efficiently and accurately converting hierarchical data between a flat, indentation-based text format and a structured tree representation, while strictly adhering to specific formatting rules and O(N) performance constraints for both parsing and serialization.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement menu parser and serializer
Menu Parser and Serializer
You are tasked with implementing logic to convert a restaurant menu between a plain text format and a tree data structure. The text format uses indentation to represent the hierarchy of categories and items.
Text Format
- The menu is provided as a multi-line string, where each non-empty line describes a single menu item.
- Leading indentation, consisting of zero or more groups of two spaces, indicates the nesting depth relative to top-level categories.
- Following the indentation, fields on each line are separated by the
|character in the format:<type>|<id>|<name>|<price>.type: A string label (e.g.,CATEGORY,FOOD,SIDE). Do not assume only these three types; treat it as an arbitrary string.id: A unique identifier string for the item.name: Free text that is guaranteed not to contain the|character.price: A decimal number (e.g.,5.99), or the literal-if the item does not have a price (e.g., a category).
Example Input:
CATEGORY|c1|Burgers|-
FOOD|f1|Classic Burger|5.99
SIDE|s1|Fries|1.99
FOOD|f2|Veggie Burger|6.49
CATEGORY|c2|Drinks|-
FOOD|f3|Cola|1.50
FOOD|f4|Water|1.00
This example demonstrates a hierarchy like category > food > side, but the general depth and item types can vary.
Tree Structure
Define a language-agnostic data structure, MenuItem, with at least the following fields:
type: stringid: stringname: stringprice: nullable/optional number (should benullif the text price is-)children: An ordered list or array ofMenuItemobjects, representing nested items.
Top-level menu items are those with zero indentation in the text format.
Tasks
parseMenu(text): Implement a function that accepts the menu text string and returns the corresponding tree. The output should be a list/array of top-levelMenuItemnodes, with allchildrenfields properly populated.serializeMenu(items): Implement a function that accepts the tree (the list/array of top-levelMenuItemnodes) and returns a canonical text representation. This output must exactly match the format described above.
Serializer Requirements:
- Use exactly two spaces for each level of indentation deeper than a parent.
- Use
|as the field separator. - Output
-for items whosepriceisnullor absent. - Output a depth-first traversal, ensuring all children of a node appear immediately after their parent, in the order they are stored in the
childrenlist.
Assumptions and Constraints:
- You may assume the input text is always a valid, well-formed tree (i.e., no cycles, consistent indentation, and at least one top-level item).
- Design your parsing and serialization logic to run in O(N) time and O(N) extra space, where N is the number of menu items (lines) in the input.