Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

String Basics and Memory Representation in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of String Concatenation and Length
What is the output of the following TypeScript code?
DSA Typescript
const s1 = "hello";
const s2 = "world";
const s3 = s1 + " " + s2;
console.log(s3.length);
A11
B10
C5
D0
Attempts:
2 left
💡 Hint
Count all characters including the space between words.
❓ Predict Output
intermediate
2:00remaining
String Immutability Effect
What will be printed after running this TypeScript code?
DSA Typescript
let str = "abc";
str[0] = "z";
console.log(str);
AZbc
Bzbc
CError
Dabc
Attempts:
2 left
💡 Hint
Strings cannot be changed by index assignment in TypeScript.
❓ Predict Output
advanced
2:00remaining
Memory Representation of String Characters
What is the output of this TypeScript code that prints character codes?
DSA Typescript
const s = "Hi!";
const codes = [];
for (let i = 0; i < s.length; i++) {
  codes.push(s.charCodeAt(i));
}
console.log(codes);
A[72, 105, 33]
B[104, 105, 33]
C[72, 105, 35]
D[104, 105, 35]
Attempts:
2 left
💡 Hint
Use ASCII codes: 'H' = 72, 'i' = 105, '!' = 33.
🧠 Conceptual
advanced
2:00remaining
Understanding String Interning
Which statement about string interning in TypeScript/JavaScript is true?
AString interning automatically merges strings with different content.
BStrings are immutable but do not guarantee shared memory references for identical content.
CStrings can be changed after creation if interned.
DStrings with the same content always share the same memory reference.
Attempts:
2 left
💡 Hint
Think about immutability and memory sharing behavior in JavaScript strings.
🔧 Debug
expert
2:00remaining
Why does this string slicing code produce unexpected output?
Consider this TypeScript code snippet. What is the output and why?
DSA Typescript
const s = "abcdef";
const slice1 = s.slice(2, 4);
const slice2 = s.slice(4, 2);
console.log(slice1);
console.log(slice2);
A"cd" and "ef" because slice swaps start and end if start > end
B"cd" and "" because slice treats end as 0 if less than start
C"cd" and "" because slice with start > end returns empty string
D"cd" and "" because slice ignores invalid indices
Attempts:
2 left
💡 Hint
Check how slice behaves when the start index is greater than the end index.

Practice

(1/5)
1. What does the term immutable mean when talking about strings in TypeScript?
easy
A. Once created, the string cannot be changed.
B. The string can be changed anytime.
C. The string is stored in a mutable array.
D. The string is stored as a number.

Solution

  1. Step 1: Understand string immutability

    Strings in TypeScript cannot be changed after creation. Any modification creates a new string.
  2. Step 2: Compare options with definition

    Only Once created, the string cannot be changed. correctly states that strings cannot be changed once created.
  3. Final Answer:

    Once created, the string cannot be changed. -> Option A
  4. Quick Check:

    Immutable means unchangeable = A [OK]
Hint: Remember: strings can't be changed after creation [OK]
Common Mistakes:
  • Thinking strings can be changed like arrays
  • Confusing mutable and immutable
  • Assuming strings are stored as numbers
2. Which of the following is the correct way to declare a string variable in TypeScript?
easy
A. let name: number = 'Alice';
B. let name = 123;
C. let name: string = 'Alice';
D. let name: string = 123;

Solution

  1. Step 1: Check variable declaration syntax

    TypeScript uses let variableName: type = value; syntax.
  2. Step 2: Match type and value

    let name: string = 'Alice'; declares a string variable with a string value correctly.
  3. Final Answer:

    let name: string = 'Alice'; -> Option C
  4. Quick Check:

    Correct string declaration = A [OK]
Hint: Match variable type with value type exactly [OK]
Common Mistakes:
  • Assigning number to string type
  • Missing type annotation
  • Using wrong syntax for declaration
3. What will be the output of the following TypeScript code?
let greeting = 'Hello';
greeting[0] = 'Y';
console.log(greeting);
medium
A. Yello
B. undefined
C. Error: Cannot assign to string index
D. Hello

Solution

  1. Step 1: Understand string immutability in TypeScript

    Strings cannot be changed by index assignment; such operations do not modify the string.
  2. Step 2: Analyze the code behavior

    Assigning greeting[0] = 'Y' does nothing; greeting remains 'Hello'.
  3. Final Answer:

    Hello -> Option D
  4. Quick Check:

    Strings immutable, no change = Hello [OK]
Hint: Strings can't be changed by index assignment [OK]
Common Mistakes:
  • Expecting first letter to change
  • Thinking string is mutable like array
  • Assuming error will be thrown
4. What best describes the result of the following TypeScript code snippet:
let str: string = 'Test';
str[1] = 'a';
console.log(str);
medium
A. No error, output is 'Test'
B. TypeError: Cannot assign to string index
C. SyntaxError: Invalid assignment
D. ReferenceError: str is not defined

Solution

  1. Step 1: Check string mutability rules

    Strings are immutable; assigning to an index does nothing.
  2. Step 2: Analyze the code behavior

    str[1] = 'a' does nothing; str remains 'Test', console.log outputs 'Test'. No error.
  3. Final Answer:

    No error, output is 'Test' -> Option A
  4. Quick Check:

    Strings immutable, index assignment ignored = 'Test' [OK]
Hint: Strings cannot be changed by index assignment [OK]
Common Mistakes:
  • Assuming no error and string changes
  • Confusing syntax error with runtime error
  • Expecting ReferenceError
5. Given the following code, what will be the output?
let s1 = 'abc';
let s2 = s1 + 'd';
console.log(s1);
console.log(s2);

Choose the correct output considering string immutability and memory representation.
hard
A. abc
abc
B. abc
abcd
C. abcd
abcd
D. Error at runtime

Solution

  1. Step 1: Understand string concatenation and immutability

    Concatenating strings creates a new string; original strings remain unchanged.
  2. Step 2: Trace variable values after operations

    s1 remains 'abc'; s2 becomes 'abcd'.
  3. Final Answer:

    abc
    abcd
    -> Option B
  4. Quick Check:

    Original unchanged, new string created = abc and abcd [OK]
Hint: Concatenation creates new string; originals stay same [OK]
Common Mistakes:
  • Expecting s1 to change after concatenation
  • Thinking concatenation modifies original string
  • Assuming runtime error on concatenation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard