Code Logo

Festival Drum Echoes

Published at05 Jan 2026
Easy 7 views
Like19

In Festival Drum Echoes, you are given a sound string and a number n. Your job is to return that sound repeated exactly n + 1 times, with a single space between each copy.

The tested behavior here is simple string construction. If n = 0, you should return the sound once. If n = 1, you should return it twice. If n = 4, you should return it five times. The spaces matter too, because the expected output is one line like "X X X X X", not a list and not a string with extra spacing.

For example, sound = "Boom" and n = 1 should produce "Boom Boom". The input sound = "Clap" and n = 0 should produce "Clap". The input sound = "Ti" and n = 2 should produce "Ti Ti Ti".

So the task is to repeat the sound the correct number of times and join the copies with single spaces.

Example Input & Output

Example 1
Input
sound = "Clap", n = 0
Output
Clap
Explanation

When n is 0, the sound is still included once.

Example 2
Input
sound = "Ti", n = 2
Output
Ti Ti Ti
Explanation

The sound is repeated n plus one times, so Ti appears 3 times.

Example 3
Input
sound = "Boom", n = 1
Output
Boom Boom
Explanation

Because n is 1, the sound appears 2 times in the final string.

Algorithm Flow

Recommendation Algorithm Flow for Festival Drum Echoes
Recommendation Algorithm Flow for Festival Drum Echoes

Solution Approach

A clean way to solve this problem is to first determine how many times the sound should appear, then build the final string by repeating that sound and joining the copies with spaces. The rule from the tests is very direct: the output should contain the sound exactly n + 1 times.

The + 1 detail matters. It is what makes n = 0 produce one copy instead of zero, n = 1 produce two copies, and so on. If you miss that detail, every example shifts by one and the result will be wrong. So the first thing the solution should do is calculate the number of repetitions as count = n + 1.

In JavaScript, one straightforward implementation looks like this:

const parts = [];
for (let i = 0; i < n + 1; i++) {
  parts.push(sound);
}
return parts.join(' ');

This works because the array parts stores one copy of the sound for each repetition. After the loop, join(' ') creates the final string with exactly one space between neighboring copies and no extra space at the beginning or end.

You could also use a helper such as Array(n + 1).fill(sound).join(' '), but the loop version is often easier to explain because it shows the repeated-building process step by step. Both versions follow the same idea: create n + 1 copies and then combine them into one spaced string.

This approach naturally handles all edge cases from the tests. When n = 0, the loop runs once and the result is just the original sound. When n = 4, the loop runs five times, giving five copies separated by spaces. It also works for very short strings like "A" and for longer words like "Boom" because the logic does not depend on the length of the sound.

The time complexity is O(k), where k is the number of repetitions, because you create each copy once. The space complexity is also O(k) for the list of parts and the final output string.

So the full strategy is: compute n + 1, create that many copies of the sound, join them with single spaces, and return the resulting echo string.

Best Answers

java
import java.util.Arrays;
class Solution {
    public boolean drum_pattern(Object nums) {
        int[] arr = (int[]) nums;
        if (arr.length < 2 || arr.length % 2 != 0) {
            return false;
        }
        int h = arr.length / 2;
        return Arrays.equals(Arrays.copyOfRange(arr, 0, h), Arrays.copyOfRange(arr, h, arr.length));
    }
}