Embercode
✕
Tries (C++)Quick quiz

Palindrome pairs and IP prefixes

8 questions. Covers IP longest prefix match with a binary trie.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <algorithm> #include <iostream> #include <string> using namespace std; bool isPalindrome(const string& s) { string reversed = s; reverse(reversed.begin(), reversed.end()); return s == reversed; } int main() { string first = "ab"; string second = "a"; string joined = first + second; cout << joined << (isPalindrome(joined) ? " yes" : " no") << endl; joined = second + first; cout << joined << (isPalindrome(joined) ? " yes" : " no") << endl; } ``
  2. 2. What does this program print? ``cpp #include <cstdint> #include <iostream> using namespace std; int bitAt(uint32_t value, int i) { return (value >> (31 - i)) & 1; } int main() { uint32_t addr = (10u << 24) | (1u << 16) | (2u << 8) | 7u; // 10.1.2.7 for (int i = 0; i < 12; ++i) cout << bitAt(addr, i); cout << endl; } ``
  3. 3. Why does the palindrome-pairs program store each word reversed in the trie?
  4. 4. A routing table holds 100,000 rules. At most how many bit steps does one lookup take in the binary trie?
  5. 5. Put the steps of addRoute in the order the code runs them, for a rule such as 10.1.0.0/16 with next hop eth2.

    Use the arrows to put the steps in order.

    1. 1.For each of the first length bits, follow the child for that bit, creating it if it is missing
    2. 2.Start at the root node
    3. 3.Store the next hop at the node reached after length bits

4 question(s) left on this page.