Code Logo

Echoing Story Weaver

Published at05 Jan 2026
Array Manipulation Easy 11 views
Like8

You can think of this as a small game with a very specific goal. In Echoing Story Weaver, you are trying to work toward the right answer by following one clear idea.

Weave layered echo story recursively A good way to think about it is to first understand what goes in, then what rule you must follow, and finally what shape the answer should have.

For example, if the input is base = "A river always remembers.", voices = ["Sela", "Varo", "Nyx"], the answer is . Example with input: base = "A river always remembers.", voices = ["Sel Another example is base = "The hearth keeps its promise.", voices = [], which gives . Example with input: base = "The hearth keeps its promise.", voices = [This is a friendly practice problem, but it still rewards careful reading. The key is understanding the rule clearly and then applying it carefully.

Example Input & Output

Example 1
Input
base = "A river always remembers.", voices = ["Sela", "Varo", "Nyx"]
Output
Explanation

Example with input: base = "A river always remembers.", voices = ["Sel

Example 2
Input
base = "The hearth keeps its promise.", voices = []
Output
Explanation

Example with input: base = "The hearth keeps its promise.", voices = [

Example 3
Input
base = "The hearth keeps its promise.", voices = ["Mira", "Orin"]
Output
Explanation

Example with input: base = "The hearth keeps its promise.", voices = [

Algorithm Flow

Recommendation Algorithm Flow for Echoing Story Weaver

Solution Approach

This problem is about building a layered echo from a list of voices. The key idea is to arrange the lines in a specific order: every voice announces itself with an opening line, then the base text appears, and finally each voice closes with its own note — but the closing notes appear in reverse order.

A simple and clear way to solve it is to build an array of lines and join them at the end. This avoids messy string concatenation and keeps the order easy to read. We do three small passes: collect the opening lines, add the base, then add the closing notes in reverse.

We start by handling the simplest case:

function echoing_story_weaver(base, voices) {
    if (voices.length === 0) return base;
    const lines = [];
    for (const voice of voices) {
        lines.push(`${voice.name} begins:`);
    }
    lines.push(base);
    for (let i = voices.length - 1; i >= 0; i--) {
        lines.push(`${voices[i].name} notes: ${voices[i].insight}`);
    }
    return lines.join("\n");
}

The base case is first: if there are no voices, the echo is just the base text itself, so we return it directly.

Otherwise, we fill lines in order. The first loop pushes name begins: for every voice in the given order. After that we push the base line. The second loop then walks the voices from the last one back to the first, appending name notes: insight for each — this reversal is what creates the "echo" that returns in the opposite direction.

Let us trace base = "A river always remembers." with voices ["Sela", "Varo", "Nyx"]. The first loop produces Sela begins:, Varo begins:, and Nyx begins:. Then the base line is added. Finally the reverse loop appends Nyx notes: ..., Varo notes: ..., and Sela notes: .... Joining these with newlines gives the finished layered diary.

The time complexity is O(n) because each voice is visited a constant number of times, and the space complexity is also O(n) to store the output lines.

Best Answers

java
class Observer {
    String name;
    String insight;

    Observer(String name, String insight) {
        this.name = name;
        this.insight = insight;
    }
}

class Solution {
    public String echoing_story_weaver(String base, Observer[] voices) {
        if (voices.length == 0) return base;
        StringBuilder sb = new StringBuilder();
        for (Observer voice : voices) {
            sb.append(voice.name).append(" begins:\n");
        }
        sb.append(base);
        for (int i = voices.length - 1; i >= 0; i--) {
            sb.append("\n").append(voices[i].name).append(" notes: ").append(voices[i].insight);
        }
        return sb.toString();
    }
}