Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Array Insertion at Beginning in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting elements at the beginning?
Consider the following TypeScript code that inserts elements at the beginning of an array. What will be the final state of the array printed?
DSA Typescript
let arr: number[] = [3, 4, 5];
arr.unshift(2);
arr.unshift(1);
console.log(arr);
A[5, 4, 3, 2, 1]
B[3, 4, 5, 1, 2]
C[2, 1, 3, 4, 5]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Remember that unshift adds elements to the start of the array in the order they are called.
❓ Predict Output
intermediate
2:00remaining
What is the output after multiple unshift calls?
What will be printed after running this TypeScript code?
DSA Typescript
const arr: string[] = ['c', 'd'];
arr.unshift('b');
arr.unshift('a');
console.log(arr.join(' -> '));
Aa -> b -> c -> d
Bb -> a -> c -> d
Cc -> d -> a -> b
Dd -> c -> b -> a
Attempts:
2 left
💡 Hint
Check the order elements are added with unshift.
🔧 Debug
advanced
2:00remaining
Why does this code not insert at the beginning as expected?
This TypeScript code tries to insert 0 at the beginning of the array but fails. What is the error?
DSA Typescript
let numbers = [1, 2, 3];
numbers.push(0);
console.log(numbers);
AThe code inserts 0 at the end, not the beginning, because push adds to the end.
BSyntaxError because push is used incorrectly.
CTypeError because numbers is not an array.
DThe array remains unchanged.
Attempts:
2 left
💡 Hint
Check the difference between push and unshift methods.
🧠 Conceptual
advanced
2:00remaining
What is the time complexity of inserting at the beginning of an array?
Inserting an element at the beginning of a standard array requires shifting existing elements. What is the time complexity of this operation?
AO(1) - constant time
BO(log n) - logarithmic time
CO(n) - linear time
DO(n^2) - quadratic time
Attempts:
2 left
💡 Hint
Think about how many elements must move when inserting at the start.
🚀 Application
expert
3:00remaining
How to efficiently insert multiple elements at the beginning?
You want to insert multiple elements ['x', 'y', 'z'] at the beginning of a large array efficiently. Which approach is best in TypeScript?
AUse multiple unshift calls: arr.unshift('x'); arr.unshift('y'); arr.unshift('z');
BConcatenate arrays: arr = ['x', 'y', 'z'].concat(arr);
CUse push with spread: arr.push(...['x', 'y', 'z']);
DUse unshift with spread: arr.unshift(...['x', 'y', 'z']);
Attempts:
2 left
💡 Hint
Consider performance and readability for large arrays.

Practice

(1/5)
1. What does inserting an element at the beginning of an array do to the existing elements?
easy
A. Replaces the first element with the new element
B. Deletes the last element of the array
C. Shifts all existing elements one position to the right
D. Leaves the array unchanged

Solution

  1. Step 1: Understand array insertion at beginning

    When a new element is inserted at the start, all existing elements move one step to the right to make space.
  2. Step 2: Effect on existing elements

    This shifting means the first element moves to second position, second to third, and so on.
  3. Final Answer:

    Shifts all existing elements one position to the right -> Option C
  4. Quick Check:

    Insert at start shifts elements right [OK]
Hint: New start element pushes others right [OK]
Common Mistakes:
  • Thinking insertion replaces first element
  • Assuming array size decreases
  • Believing array stays unchanged
2. Which of the following is the correct TypeScript syntax to insert an element `5` at the beginning of array `arr`?
easy
A. arr.unshift(5);
B. arr.addFirst(5);
C. arr.insert(0, 5);
D. arr.push(5);

Solution

  1. Step 1: Recall array methods for insertion

    In TypeScript (JavaScript), `unshift` adds elements at the start of an array.
  2. Step 2: Check other options

    `push` adds at the end, `insert` and `addFirst` are not valid array methods.
  3. Final Answer:

    arr.unshift(5); -> Option A
  4. Quick Check:

    Use unshift() to add at beginning [OK]
Hint: Use unshift() to add at start [OK]
Common Mistakes:
  • Using push() which adds at the end
  • Using non-existent methods like insert()
  • Confusing unshift() with pop()
3. What will be the output of the following TypeScript code?
let arr = [10, 20, 30];
arr.unshift(5);
console.log(arr);
medium
A. [10, 20, 30, 5]
B. [5, 10, 20, 30]
C. [5, 20, 30]
D. [10, 5, 20, 30]

Solution

  1. Step 1: Understand unshift effect

    unshift(5) adds 5 at the start, shifting existing elements right.
  2. Step 2: Resulting array

    Original [10, 20, 30] becomes [5, 10, 20, 30].
  3. Final Answer:

    [5, 10, 20, 30] -> Option B
  4. Quick Check:

    unshift adds at front [OK]
Hint: unshift adds element at front [OK]
Common Mistakes:
  • Thinking unshift adds at end
  • Misplacing the new element in the array
  • Confusing unshift with push
4. Identify the error in the following TypeScript code that tries to insert `100` at the beginning of array `nums`.
let nums = [1, 2, 3];
nums.add(0, 100);
console.log(nums);
medium
A. `add` is not a valid method for arrays
B. `console.log` is used incorrectly
C. Array 'nums' is not declared properly
D. The element 100 cannot be added to arrays

Solution

  1. Step 1: Check array method usage

    Arrays in TypeScript do not have an `add` method; insertion at beginning uses `unshift`.
  2. Step 2: Validate other parts

    Declaration and console.log are correct; adding 100 is allowed.
  3. Final Answer:

    `add` is not a valid method for arrays -> Option A
  4. Quick Check:

    Use unshift(), not add() [OK]
Hint: Use unshift(), not add() for insertion [OK]
Common Mistakes:
  • Using non-existent add() method
  • Thinking console.log is wrong
  • Believing arrays can't hold numbers
5. Given an empty array `let data: number[] = [];`, which sequence of operations will result in the array `[3, 2, 1]` by inserting elements only at the beginning?
hard
A. data.unshift(1); data.unshift(2); data.unshift(3);
B. data.push(1); data.push(2); data.push(3);
C. data.push(3); data.unshift(2); data.push(1);
D. data.unshift(3); data.unshift(2); data.unshift(1);

Solution

  1. Step 1: Understand insertion at beginning

    Each unshift adds element at front, pushing existing elements right.
  2. Step 2: Trace operations

    Starting empty: unshift(3) -> [3], unshift(2) -> [2,3], unshift(1) -> [1,2,3].
  3. Final Answer:

    data.unshift(3); data.unshift(2); data.unshift(1); -> Option D
  4. Quick Check:

    Insert in reverse order with unshift [OK]
Hint: Insert last element first with unshift [OK]
Common Mistakes:
  • Using push() which adds at end
  • Inserting elements in wrong order
  • Mixing push and unshift incorrectly
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