DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
MediumJavaScript

Your Accessibility Audit Tool Needs to Collect All Unique HTML Tag Names in a Document

1 views

Technical Interview Challenge: The "Accessibility Auditor" Tree Traversal

Context

When building accessibility (A11y) auditing tools or SEO scrapers, you often need to analyze the semantic structure of a webpage. A common first step is to inventory the types of HTML tags being used to ensure a healthy balance of semantic elements (like <main>, <article>, <nav>) versus non-semantic containers (like <div> or <span>).

At a browser level, the Document Object Model (DOM) is an N-ary tree. Efficiently traversing this tree while maintaining a record of unique nodes is a fundamental skill for building browser extensions and high-performance frontend crawlers.


Problem Statement

Implement a function getTags(root) that performs a full traversal of a DOM tree starting from a given root element. The function should collect and return an array of all unique tag names (in uppercase) present in the tree.

Requirements:

  1. Unique Collection: Each tag name should appear only once in the resulting array.

  2. Order of Encounter: The tags must be returned in the order they are first encountered during a Depth-First Search (DFS) traversal.

  3. Efficiency: The solution should handle deep or wide trees without excessive memory overhead.

  4. Null Safety: If the root is null, the function should return an empty array.


Example Use Cases

Example 1: Diverse Elements

HTML

<div id="root">
  <span>
    <p>Hello</p>
  </span>
  <p>World</p>
</div>

getTags(document.getElementById('root')); 
// Expected Output: ["DIV", "SPAN", "P"]

Example 2: Nested Lists

HTML

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

getTags(document.querySelector('ul')); 
// Expected Output: ["UL", "LI"]

Example 3: Deep Nesting

HTML

<div>
  <div>
    <div>
      <section></section>
    </div>
  </div>
</div>

getTags(document.querySelector('div')); 
// Expected Output: ["DIV", "SECTION"]

Interview Evaluation Criteria

  • Traversal Strategy: Do you use a recursive approach or an iterative approach with a stack? Can you explain the trade-offs regarding the call stack limit?

  • Data Structures: Are you using a Set to track uniqueness efficiently ($O(1)$ lookup)?

  • DOM API Knowledge: Do you know the difference between .children (Elements only) and .childNodes (includes Text and Comment nodes)?

  • Case Consistency: How do you handle the fact that element.tagName typically returns uppercase, but consistency is key for the final output?

Sample Test Cases

Case 1
Input
<div><span>text</span><p><span></span></p></div>
Expected Output
["DIV","SPAN","P"]
Case 2
Input
<ul><li></li><li></li></ul>
Expected Output
["UL","LI"]
Case 3
Input
<div></div>
Expected Output
["DIV"]

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
1
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering

Languages

JavaScript