Facebook Onsite Interview Question: getTextBetweenNodes(node1, node2)

Anonymous User
1854
Feb 07, 2021

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;<strong><code>Node</code></strong>&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 <code>Node</code> object. All objects that implement <code>Node</code> functionality are based on one of its subclasses. Most notable are
          <strong><code>Document</code></strong>, <strong><code>Element</code></strong>, and <strong><code>DocumentFragment</code></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 <code>Node</code>. These include <strong><code>Attr</code></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 (10)