Skip to main content

Practice ยท 1 of 4

StringBuilder, Not Concatenation

CHALLENGE
Difficulty: intermediate+25 XP

ConcatReport builds one big string with += inside the loop โ€” the quadratic shape. Rewrite it as Solution.Report(string[] lines) using a System.Text.StringBuilder so the loop allocates no intermediate strings: - one line per input, each terminated with '\n' (so lines = ["a","b"] produces "a\nb\n"), - empty input produces "", - and StringOps.Allocations must stay at 0 across the whole call (the counting helper is provided; call StringOps.Bump() never). ``csharp public static class Solution { public static string Report(string[] lines); // one line per input, '\n'-terminated } // provided: public static class StringOps { int Allocations; Reset(); Bump(); } ``

Back to lesson: Practice: Performance Practice: Build, Pick, Materialize