Extract a hidden message from the direct text of HTML tags whose names start with s

Quick Overview

Scan a simplified, well-formed HTML string and build a hidden message from the direct text of every tag whose name starts with s, joined by single spaces in document order. It tests stack-based parsing of nested tags and crediting each text run to its innermost enclosing tag.

Extract a hidden message from the direct text of HTML tags whose names start with s

Company: Ramp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `html` representing a simplified HTML document. The document consists of properly nested tags of the form `<tag>` (opening) and `</tag>` (closing), with plain text interleaved between them. A hidden message is encoded as follows: for every tag whose name starts with the letter `s` (case sensitive), the direct text content inside that tag (text that is not inside any deeper nested tag) contributes to the message. Return the concatenation of all such direct text fragments, in the order they appear in the document, separated by a single space. If there are no such fragments, return an empty string. Tag names consist only of lowercase English letters. Text fragments contain no `<` or `>` characters. The input is guaranteed to be well-formed: every opening tag has a matching closing tag, and tags are properly nested. ### Function Signature ```python def extract_hidden_message(html: str) -> str: ``` ### Rules - A text fragment is a maximal run of characters between two tags, or between a tag and the start or end of the string. - A fragment belongs to the innermost tag that encloses it, and it contributes only when that tag's name starts with `s`. Text outside every tag never contributes. - Each contributing fragment is trimmed of leading and trailing whitespace. A fragment that is empty after trimming is skipped. - The contributing fragments are joined with a single space in document order. Return `""` when nothing contributes. ### Constraints - `1 <= len(html) <= 10^4` - The input is a well-formed HTML-like string. - Tag names consist of lowercase English letters only. - Text fragments do not contain `<` or `>` characters. ### Examples ```text Input: html = "<div><span>hello</span><p>world</p><section>secret</section></div>" Output: "hello secret" ``` The tags whose names start with `s` are `<span>`, with direct text "hello", and `<section>`, with direct text "secret". The text "world" sits in `<p>`, so it is ignored. ```text Input: html = "<a>ignore<s>find<b>nope</b>me</s></a>" Output: "find me" ``` Inside `<s>` the direct fragments are "find" and "me". The word "nope" is inside the nested `<b>`, and "ignore" belongs to `<a>`, so neither counts. ```text Input: html = "<section> top <span>inner</span> </section><p>skip</p>" Output: "top inner" ``` The fragment " top " belongs to `<section>` and trims to "top". "inner" belongs to `<span>`. The single space after `</span>` belongs to `<section>` again but is empty after trimming, so it is skipped.

Overview: Scan a simplified, well-formed HTML string and build a hidden message from the direct text of every tag whose name starts with s, joined by single spaces in document order. It tests stack-based parsing of nested tags and crediting each text run to its innermost enclosing tag.

Ramp logo
Ramp
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
2
0

You are given a string html representing a simplified HTML document. The document consists of properly nested tags of the form <tag> (opening) and </tag> (closing), with plain text interleaved between them.

A hidden message is encoded as follows: for every tag whose name starts with the letter s (case sensitive), the direct text content inside that tag (text that is not inside any deeper nested tag) contributes to the message.

Return the concatenation of all such direct text fragments, in the order they appear in the document, separated by a single space. If there are no such fragments, return an empty string.

Tag names consist only of lowercase English letters. Text fragments contain no < or > characters. The input is guaranteed to be well-formed: every opening tag has a matching closing tag, and tags are properly nested.

Function Signature

def extract_hidden_message(html: str) -> str:

Rules

  • A text fragment is a maximal run of characters between two tags, or between a tag and the start or end of the string.
  • A fragment belongs to the innermost tag that encloses it, and it contributes only when that tag's name starts with s . Text outside every tag never contributes.
  • Each contributing fragment is trimmed of leading and trailing whitespace. A fragment that is empty after trimming is skipped.
  • The contributing fragments are joined with a single space in document order. Return "" when nothing contributes.

Constraints

  • 1 <= len(html) <= 10^4
  • The input is a well-formed HTML-like string.
  • Tag names consist of lowercase English letters only.
  • Text fragments do not contain < or > characters.

Examples

Input: html = "<div><span>hello</span><p>world</p><section>secret</section></div>"
Output: "hello secret"

The tags whose names start with s are <span>, with direct text "hello", and <section>, with direct text "secret". The text "world" sits in <p>, so it is ignored.

Input: html = "<a>ignore<s>find<b>nope</b>me</s></a>"
Output: "find me"

Inside <s> the direct fragments are "find" and "me". The word "nope" is inside the nested <b>, and "ignore" belongs to <a>, so neither counts.

Input: html = "<section> top <span>inner</span> </section><p>skip</p>"
Output: "top inner"

The fragment " top " belongs to <section> and trims to "top". "inner" belongs to <span>. The single space after </span> belongs to <section> again but is empty after trimming, so it is skipped.