Embercode
✕
Heaps and Priority QueuesQuick quiz

Scheduler and reorganising

8 questions. Covers Reorganise a string with a max heap.

Questions 1 to 5 of 8

  1. 1. What does this program print? ``cpp #include <algorithm> #include <array> #include <iostream> #include <string> int leastIntervalFormula(const std::string& tasks, int n) { std::array<int, 26> freq{}; for (char c : tasks) freq[c - 'A']++; int maxFreq = *std::max_element(freq.begin(), freq.end()); int countMax = 0; for (int f : freq) if (f == maxFreq) countMax++; int frames = (maxFreq - 1) * (n + 1) + countMax; return std::max(static_cast<int>(tasks.size()), frames); } int main() { std::cout << "AABBCC n=0: " << leastIntervalFormula("AABBCC", 0) << "\n"; std::cout << "AAAB n=1: " << leastIntervalFormula("AAAB", 1) << "\n"; } ``
  2. 2. What does this program print? Separate the three output lines with / . ``cpp #include <iostream> #include <queue> #include <utility> int main() { std::priority_queue<std::pair<int, char>> heap; heap.push({3, 'a'}); heap.push({1, 'b'}); heap.push({2, 'c'}); while (!heap.empty()) { auto [copies, letter] = heap.top(); heap.pop(); std::cout << letter << " " << copies << "\n"; } } ``
  3. 3. What does this program print? ``cpp #include <array> #include <iostream> #include <queue> #include <string> #include <utility> std::string reorganize(const std::string& s) { std::array<int, 26> freq{}; for (char c : s) freq[c - 'a']++; std::priority_queue<std::pair<int, char>> heap; for (int i = 0; i < 26; ++i) if (freq[i] > 0) heap.push({freq[i], static_cast<char>('a' + i)}); std::string out; std::pair<int, char> held{0, '?'}; while (!heap.empty()) { auto [copies, letter] = heap.top(); heap.pop(); out += letter; if (held.first > 0) heap.push(held); held = {copies - 1, letter}; } if (held.first > 0) return ""; return out; } int main() { std::cout << reorganize("aabbcc") << "\n"; } ``
  4. 4. In the scheduler, a task runs in slot t with cooldown n. In which slot is it first allowed to run again?
  5. 5. Which test decides whether a string of length n can be rearranged so that no two equal letters touch? maxFreq is the largest letter count.

5 question(s) left on this page.