Query HTML Format String
Filed under Amazon
Free read 1 of 3Use a stack to track open tags while iterating through the string.
The question
Given an HTML-formatted string, implement a parser to retrieve the content of specific tags or attributes based on a query (e.g., 'GET tag_name').
Constraints
- Input string is well-formed HTML.
- Queries are simple key-value lookups.
- Must handle nested tags.
Examples
- Input
- String: '<div><name>John</name></div>', Query: 'GET name'
- Output
- John
- Why
- Extracts the text content within the <name> tag.
Approach
Use a stack to track open tags while iterating through the string.
Identify tag boundaries using '<' and '>' characters.
Once the target tag is identified, extract the inner text until the corresponding closing tag is reached.
Tips
- What they are testing: Tests stack-based parsing and string manipulation.
- Common mistake: Using regex for parsing HTML, which is brittle and fails on nested structures.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.