ProgrammingHardCompetitive coding (CP)
Starting at node 0, how many nodes does this linked walk visit before it reaches the sentinel -1? The next-index array is {3, 2, 0, -1}.
Starting at node 0, how many nodes does this linked walk visit before it reaches the sentinel -1? The next-index array is {3, 2, 0, -1}.
#include <stdio.h>
int main(void) {
int nxt[] = {3, 2, 0, -1};
int i = 0, cnt = 0;
while (i != -1) {
cnt++;
i = nxt[i];
}
printf("%d", cnt);
}- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: B. 2
The walk goes from node 0 to node 3 and then reads -1 from node 3, so it visits 2 nodes counting the start; nodes 1 and 2 are never reached.
More C Programming questions
- What is the digital root of 987654 (the repeated digit sum until one digit remains)? The recursion computes it. What is printed? c…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 5; printf("%d", (x - 6) > 0); }
- How many inner iterations run in total? Compute with this program. c #include <stdio.h> int main(void) { int i = 0, c = 0; do { for (int j…
- What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in…
- What is printed? c #include <stdio.h> long long sum(long long n, long long acc) { return n == 0 ? acc : sum(n - 1, acc + n); } int…
- How many odd multiples of 3 are there in 1 to 30? Count them with this program. c #include <stdio.h> int main(void) { int c = 0; for…