0
0
DSA Typescriptprogramming~10 mins

Recursion on Arrays and Strings in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Recursion on Arrays and Strings
Start with full array/string
Check base case: empty or single element?
YesReturn base result
No
Process first element
Call recursion on rest of array/string
Combine first element with recursive result
Return combined result
Recursion breaks the array or string into smaller parts, processes one element, then calls itself on the rest until a base case is reached.
Execution Sample
DSA Typescript
function sumArray(arr: number[]): number {
  if (arr.length === 0) return 0;
  return arr[0] + sumArray(arr.slice(1));
}
This function sums all numbers in an array by adding the first element to the sum of the rest recursively.
Execution Table
StepOperationArray StateRecursive Call ArgumentReturn ValueVisual State
1Call sumArray([1,2,3])[1,2,3][2,3]pendingsumArray([1,2,3]) waiting for sumArray([2,3])
2Call sumArray([2,3])[2,3][3]pendingsumArray([2,3]) waiting for sumArray([3])
3Call sumArray([3])[3][]pendingsumArray([3]) waiting for sumArray([])
4Call sumArray([])[]N/A0Base case reached, returns 0
5Return from sumArray([])[]N/A0sumArray([3]) computes 3 + 0 = 3
6Return from sumArray([3])[3][]3sumArray([2,3]) computes 2 + 3 = 5
7Return from sumArray([2,3])[2,3][3]5sumArray([1,2,3]) computes 1 + 5 = 6
8Return from sumArray([1,2,3])[1,2,3][2,3]6Final result 6 returned
💡 Recursion stops when array is empty (base case), returning 0.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
arr[1,2,3][1,2,3][2,3][3][][][3][2,3][1,2,3]
Return ValueN/Apendingpendingpending03566
Key Moments - 3 Insights
Why does the function call itself with arr.slice(1) instead of arr?
Because each recursive call must work on a smaller part of the array to reach the base case. The execution_table rows 1-4 show how the array gets smaller each time.
What is the base case and why is it important?
The base case is when the array is empty (arr.length === 0), returning 0 immediately. This stops infinite recursion. See step 4 in the execution_table where the base case returns 0.
How does the function combine results from recursive calls?
After the recursive call returns, the function adds the first element to the returned sum. This is shown in steps 5-7 where return values are combined step-by-step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the return value and why?
A0, because the array is empty (base case)
B3, because it's the last element
Cundefined, because no return statement
D1, because it's the first element
💡 Hint
Check the 'Return Value' and 'Operation' columns at Step 4 where the base case is reached.
At which step does the function start combining the first element with the recursive result?
AStep 2
BStep 5
CStep 1
DStep 8
💡 Hint
Look at the 'Return Value' and 'Visual State' columns where the function returns from sumArray([]) and adds 3 + 0.
If the input array was empty from the start, how many steps would the execution_table have?
A3 steps
B2 steps
C1 step
DNo steps
💡 Hint
Refer to the base case in the execution_table where the array is empty and the function returns immediately.
Concept Snapshot
Recursion on Arrays and Strings:
- Base case: empty or single element array/string
- Process first element
- Recursively call on rest (slice(1))
- Combine first element with recursive result
- Stops when base case reached
- Useful for sums, searches, reversals
Full Transcript
Recursion on arrays and strings works by breaking the problem into smaller parts. We check if the array or string is empty or has one element, which is the base case. If yes, we return a simple result. Otherwise, we process the first element and call the function again on the rest of the array or string. This continues until the base case is reached. Then, results are combined as the recursive calls return. For example, summing an array adds the first element to the sum of the rest recursively. The execution table shows each call and return, with the array shrinking each time until empty. This method helps solve problems by dividing them into smaller, easier steps.