Start learning free

LESSON 73 OF 291 · STRINGS - CHALLENGES

Burrrrp in Kotlin: Building Strings with repeat

Practice Read the full explanation

Given a number n, return the sound of a burp with n letters r: "bu", then n letters r, then "p".

 

Example 1:

Input: n = 4
Output: "burrrrp"
Explanation: "bu", then 4 letters r, then "p". That's the burp in this lesson's title.

Example 2:

Input: n = 1
Output: "burp"
Explanation: The shortest burp has a single r.

Example 3:

Input: n = 7
Output: "burrrrrrrp"
Explanation: "bu", then 7 letters r, then "p".

 

Constraints:

  • n is a whole number from 1 to 20.
  • The result is all lowercase.
STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · STRINGS - CHALLENGES

    Burrrrp in Kotlin, explained

    Problem overview

    The function burrrrp receives a number n and returns the sound of a burp: "bu", then n letters r, then "p".

    For n = 4 the answer is "burrrrp". For n = 1 it is "burp", and for n = 7 it is "burrrrrrrp".

    Approach

    Only the number of r letters changes. "bu" and "p" are always the same, so the result is a fixed start, a repeated middle and a fixed end. In Kotlin, "r".repeat(n) returns "r" written n times, and + joins the three parts.

    Step by step

    1. Start with the fixed text "bu".
    2. Add "r".repeat(n), the middle part.
    3. Add the fixed ending "p" and return the result.

    The solution in Kotlin

    class Solution {
        fun burrrrp(n: Int): String {
            return "bu" + "r".repeat(n) + "p"
        }
    }
    

    Details worth knowing:

    • repeat is a function on String. "ab".repeat(3) is "ababab". It builds a new string and doesn't change the original.
    • Zero and negative counts. "r".repeat(0) is the empty string, so burrrrp(0) would give "bup". A negative count throws an IllegalArgumentException. The lesson keeps n between 1 and 20.
    • Use a string, not a character. "r" has double quotes, so it is a String. A single-quoted 'r' is a Char, which has no repeat.
    • A template works too. "bu${"r".repeat(n)}p" builds the same text.

    Tracing the examples

    For n = 4: "r".repeat(4) is "rrrr". The three parts "bu", "rrrr" and "p" join into "burrrrp".

    For n = 1: "r".repeat(1) is "r", and the result is "burp", the word with a single r.

    For n = 7: the middle is seven r letters, and the result is "burrrrrrrp".

    The pattern is worth remembering: when part of the output is fixed and part depends on a number, write the fixed parts as literals and compute only the changing part. The same shape, literal plus repeated piece plus literal, fits many string-building exercises.

    The largest input, n = 20, produces a middle of 20 letters. repeat handles that in one call, with no loop to write and no counter to get wrong.

    Where the pieces come from

    Each part of the result has its own source. The two literals are text you type. The middle is computed from the input, so it is the only place where n appears in the code. Keeping the number in one place makes the function easy to check: if the output has the wrong number of r letters, the mistake can only be in the argument to repeat.

    Why not a loop

    A beginner's first idea is often a loop that appends one r at a time to a variable. That works, but it needs a mutable variable, a counter and a condition, and each of them is a place for an off-by-one mistake. repeat does the same work in one expression. The result is a new String built in a single call, which also keeps the function body to one line.

    Loops are still the right tool when each step differs from the last. Here every step adds the same letter, so a library function that says "this text, n times" matches the task exactly.

    Edge cases and common mistakes

    • Repeating the wrong piece. "bur".repeat(n) repeats the b and u as well. Only the r belongs in the repeat.
    • Off by one. repeat(n - 1) gives one r too few: for n = 4 the result would be "burrrp".
    • Smallest input. With n = 1 the result is "burp", which already has one r.

    Testing

    • n = 4 gives "burrrrp".
    • n = 1 gives "burp".
    • n = 7 gives "burrrrrrrp".
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free