Facebook Onsite Interview Question: getTextBetweenNodes(node1, node2)

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2021-02-07Added By Anonymous
Source: Link

Company: Meta (Facebook)
Role: e5
Interview Stage: Onsite
Location: unknown
Year: unknown

Original Post Content:

I just had my Facebook onsite for a Senior Frontend Engineer role. I got this question and couldn\'t figure out how to answer it. Can anybody help me figure out how to solve this so I can learn it for my future interviews? Thanks.

QUESTION

Given two references to two DOM nodes, return the text between them.

Assume you have access to:

1. Node\'s children as an array: node.children
2. Node\'s parent: node.parentNode
3. Get text content of a node (naive approach): node.textContent

EXAMPLE

Given this HTML:

<html>
  <head>
  </head>
  <body>
    <main>
      <section>
        Text that shouldn\'t be in the output
        <div>Text that shouldn\'t be in the output</div>
        <header>
          Text that shouldn\'t be in the output
          <h1>Text that shouldn\'t be in the output</h1>
          <h2>On this Page</h2> <!-- node1 -->
          <button>
            Jump to section
          </button>
        </header>
        <ul>
          <li><span><span><span><span><span><span>Properties</span></span></span></span></span></span></li>
          <li><span><span><span><span><span><span>Methods</span></span></span></span></span></span></li>
          <li><span><span><span><span><span><span>Examples</span></span></span></span></span></span></li>
          <li><span><span><span><span><span><span>Specifications</span></span></span></span></span></span></li>
          <li><span><span><span><span><span><span>Browser compatibility</span></span></span></span></span></span></li>
          <li><span><span><span><span><span><span>Related topics</span></span></span></span></span></span></li>
        </ul>
      </section>
    </main>
    <div>
      <article>
        text that should be included
        <p> <!-- node2 -->
          <span>
            The <strong>DOM</strong>&nbsp;Node&nbsp;interface is an abstract base class upon which many other DOM API objects are based, thus letting those object types to be used
            similarly and often interchangeably.
          </span>
          As an abstract class, there is no such thing as a plain Node object. All objects that implement Node functionality are based on one of its subclasses. Most notable are
          <strong>Document</strong>, <strong>Element</strong>, and <strong>DocumentFragment</strong>.
        </p>
        <p>
          Text that shouldn\'t be in the output
          In addition, every kind of DOM node is represented by an interface based on Node. These include <strong>Attr</strong>,
        </p>
      </article>
    </div>
  </body>
</html>

The output should be:

getTextBetweenNodes(node1, node2);
["On this Page", "Jump to section", "Properties", "Methods", "Examples", "Specifications", "Browser compatibility", "Related topics", "text that should be included", "The ", "DOM", "Node", "Node", "Node", "Document", "Element", "DocumentFragment"]

Comments