Challenge - 5 Problems
StringBuilder Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of StringBuilder Append and Insert
What is the output of this C# code snippet?
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder("Hello"); sb.Append(" World"); sb.Insert(5, ","); Console.WriteLine(sb.ToString()); } }
Attempts:
2 left
💡 Hint
Remember that Insert adds characters at the specified index without removing existing ones.
✗ Incorrect
The StringBuilder starts with "Hello". Append adds " World" making "Hello World". Insert at index 5 adds a comma after "Hello", resulting in "Hello, World".
❓ Predict Output
intermediate2:00remaining
StringBuilder Replace method effect
What will be printed by this code?
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder("abracadabra"); sb.Replace("a", "o", 3, 5); Console.WriteLine(sb.ToString()); } }
Attempts:
2 left
💡 Hint
Replace only affects the substring starting at index 3 for length 5.
✗ Incorrect
Replace("a", "o", 3, 5) changes 'a' to 'o' only in substring from index 3 to 7. The original string is "abracadabra". The substring "acada" (index 3 to 7) has 'a's replaced with 'o's resulting in "ocodo". So the final string is "abrocodobra".
❓ Predict Output
advanced2:00remaining
Performance difference between string concatenation and StringBuilder
What is the expected output of this code snippet?
C Sharp (C#)
using System; using System.Text; using System.Diagnostics; class Program { static void Main() { int n = 10000; Stopwatch sw = new Stopwatch(); sw.Start(); string s = ""; for (int i = 0; i < n; i++) { s += i.ToString(); } sw.Stop(); Console.WriteLine("String concat time: " + sw.ElapsedMilliseconds + "ms"); sw.Reset(); sw.Start(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.Append(i.ToString()); } string result = sb.ToString(); sw.Stop(); Console.WriteLine("StringBuilder time: " + sw.ElapsedMilliseconds + "ms"); } }
Attempts:
2 left
💡 Hint
String concatenation creates many intermediate strings, while StringBuilder modifies in place.
✗ Incorrect
String concatenation inside a loop is slow because it creates new strings each time. StringBuilder is optimized for many appends and is much faster.
❓ Predict Output
advanced2:00remaining
Effect of Capacity and Length on StringBuilder
What will be the output of this code?
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder(5); Console.WriteLine(sb.Capacity); sb.Append("HelloWorld"); Console.WriteLine(sb.Capacity); sb.Length = 5; Console.WriteLine(sb.ToString()); } }
Attempts:
2 left
💡 Hint
Capacity grows automatically when exceeded. Length truncates the string.
✗ Incorrect
Initial capacity is 5. After appending "HelloWorld" (10 chars), capacity grows to 16 (at least double the previous capacity). Setting Length to 5 truncates to "Hello".
❓ Predict Output
expert2:00remaining
StringBuilder Thread Safety and Exception
What happens when this code runs?
C Sharp (C#)
using System; using System.Text; using System.Threading.Tasks; class Program { static void Main() { StringBuilder sb = new StringBuilder(); Parallel.For(0, 1000, i => { sb.Append(i); }); Console.WriteLine(sb.Length); } }
Attempts:
2 left
💡 Hint
StringBuilder is not thread-safe for concurrent writes.
✗ Incorrect
StringBuilder is not safe to use from multiple threads without synchronization. Concurrent Append calls cause InvalidOperationException.