Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › RECURSION BASICS
For the source string "code", the function state contains the source, a separate blank destination, the length n = 4, and the current index i. One call is responsible for one position: it copies source[i] into destination[i], then makes the next call with i + 1. The call for i = 0 copies c, the call for i = 1 copies o, the call for i = 2 copies d, and the call for i = 3 copies e.
void copyString(const char* source, char* destination, int n, int i) {
if (i == n) {
return;
}
destination[i] = source[i];
copyString(source, destination, n, i + 1);
}The index moves forward only after the matching assignment has happened. Starting at i = 0 therefore produces four assignments at indices 0, 1, 2, and 3. Reaching the end does not copy the characters that came before it, so every call must perform its own assignment.
The calls for i = 0 and i = 1 have performed their assignments. What contents has the destination received so far?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The four character positions are 0 through 3. After the call for i = 3 assigns e, the next call has i = 4, which equals n. That boundary is reached only after every character position has been copied, so stopping there cannot skip a character.
In C++, the boundary call must write the string terminator to destination[4]. The terminator is not one of the four letters, but it tells C++ string output routines where the copied character array ends. In Java, a destination char array with length 4 already records its size, so the boundary call can return without writing an extra character.
if (i == n) {
destination[i] = '\0';
return;
}if (i == n) {
return;
}The destination changes as the calls advance. After i = 0, it begins with c. After i = 1, it contains co. After i = 2, it contains cod. After i = 3, it contains code. The source remains "code" throughout, while the destination receives values at matching positions.
| COMPLETED CALL | ASSIGNMENT | DESTINATION STATE |
|---|---|---|
| i = 0 | destination[0] = source[0] | "c" |
| i = 1 | destination[1] = source[1] | "co" |
| i = 2 | destination[2] = source[2] | "cod" |
| i = 3 | destination[3] = source[3] | "code" |
| i = 4 | C++ writes destination[4] = '\0' | Complete C++ string |
The algorithm does not change or replace the source reference. It writes into a different destination at each index. Recursive control flow only chooses which call runs next; it writes no characters unless the assignment destination[i] = source[i] executes.
Complete the boundary action for the C++ copy of "code" so output routines recognize the destination as a complete string.
void copyString(const char* source, char* destination, int n, int i) {
if (i == n) {
// complete this boundary action
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For a source of length n, every non-boundary call performs one character assignment and makes one smaller recursive call. The work therefore follows T(n) = T(n - 1) + Theta(1). For "code", the four character calls each do constant work, so the total time is Theta(n), which is four units of character work in this example.
The call stack contains calls with i = 0, i = 1, i = 2, i = 3, and i = 4 before the boundary returns. That is five frames for n = 4. In general, the boundary adds one frame to the n character calls, and n + 1 is Theta(n), so the auxiliary stack space is Theta(n).
i = 0 -> copy source[0] to destination[0]
i = 1 -> copy source[1] to destination[1]
i = 2 -> copy source[2] to destination[2]
i = 3 -> copy source[3] to destination[3]
i = 4 -> write '\0' in C++, or return in Java