Embercode
✕
The STLQuick quiz

Frequency and top-k

8 questions. Covers Top-k with a heap.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <iostream> #include <map> int main() { std::map<int, int> freq; for (int x : {3, 1, 3, 2, 3, 1}) ++freq[x]; std::cout << freq[7] << ' ' << freq.size() << '\n'; } ``
  2. 2. What does this program print? ``cpp #include <functional> #include <iostream> #include <queue> #include <vector> int main() { std::priority_queue<int, std::vector<int>, std::greater<int>> heap; for (int x : {42, 7, 19}) heap.push(x); heap.pop(); std::cout << heap.top() << ' ' << heap.size() << '\n'; } ``
  3. 3. Which statements about the two-sum walk in lesson 10 are correct? Pick all that apply.

    Pick every answer that is true.

  4. 4. The heap already holds k values, and a new value x arrives that beats the weakest value in the heap. Put the steps in the order the loop runs them.

    Use the arrows to put the steps in order.

    1. 1.Compare x with heap.top()
    2. 2.Call pop() to remove the weakest value
    3. 3.Call push(x) to store the new value
    4. 4.Check whether heap.size() is less than k
  5. 5. In C++, what does seen[need] do when need is not already a key in the map seen?

4 question(s) left on this page.