ProgrammingHardProduct company OA

What is printed?

What is printed?
int steps(int n) { return n == 1 ? 0 : 1 + steps(n % 2 ? 3 * n + 1 : n / 2); }
int main(void) { printf("%d %d", steps(6), steps(7)); }
  1. A. 9 17
  2. B. 8 16
  3. C. 6 16
  4. D. 8 17

Correct answer: B. 8 16

steps counts Collatz moves until 1. From 6: 3, 10, 5, 16, 8, 4, 2, 1, which is 8 moves. From 7: 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, which is 16. '9 17' counts the numbers visited instead of the moves.

More C Programming questions

Exams that ask this topic