Embercode
✕
Dynamic ProgrammingQuick quiz

Fibonacci and decode ways

8 questions. Covers Decode ways and string partition DP.

Questions 1 to 5 of 8

  1. 1. Predict the output of this program. ``cpp #include <cstdint> #include <iostream> std::uint64_t fib_two_vars(int n) { std::uint64_t prev = 0; // F(0) std::uint64_t curr = 1; // F(1) for (int i = 0; i < n; ++i) { std::uint64_t next = prev + curr; prev = curr; curr = next; } return prev; } int main() { std::cout << fib_two_vars(7) << ' ' << fib_two_vars(0) << ' ' << fib_two_vars(2) << '\n'; } ``
  2. 2. A string of digits has a 0 at the start, as in "06". Why does "06" have 0 ways to decode?
  3. 3. Predict the output of this program. It prints one line with three numbers. ``cpp #include <cstddef> #include <cstdint> #include <iostream> #include <string_view> std::uint64_t num_decodings_o1(std::string_view s) { std::uint64_t two_back = 1; // dp[i - 2] std::uint64_t one_back = 1; // dp[i - 1] for (std::size_t i = 1; i <= s.size(); ++i) { std::uint64_t current = 0; if (s[i - 1] != '0') current += one_back; if (i >= 2) { int two = (s[i - 2] - '0') * 10 + (s[i - 1] - '0'); if (two >= 10 && two <= 26) current += two_back; } two_back = one_back; one_back = current; } return one_back; } int main() { std::cout << num_decodings_o1("226") << ' ' << num_decodings_o1("1020") << ' ' << num_decodings_o1("1111") << '\n'; } ``
  4. 4. Which of these strings have 0 ways to decode? Pick all that apply.

    Pick every answer that is true.

  5. 5. In the matrix method, which entry of M to the power n holds F(n)? Give the row and column as indexes, counting from 0.

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.