← Capgemini bank

Capgemini · Question 33

Task Scheduler

C++code⏱ O(n)💾 O(1)
Service-based

Problem

You are given tasks and cooldown time n. Find minimum time to complete all tasks.

Approach

Pack the most frequent tasks into slots of size n+1.

Time O(n)Space O(1)

How it works

  1. 1Count frequencies; find the max and its multiplicity.
  2. 2Cooldown slots set the floor; total tasks may exceed it.

Sample input / output

Input:
tasks = ["A","A","A","B","B","B"], n = 2
Output:
8

Constraints

Tasks A-Z; 0 <= n <= 100.

Solution — read it, then walk it

Time O(n)Space O(1)Loops 2Branches 1Returns 2source · C++
Scroll sideways for wide charts — the view follows the active step.
↻ repeat↻ repeatSTARTinclude <iostream>include <vector>include <unordered_map>include <algorithm>using namespace stdDEFINEleastInterval(vector<char>&tasks, int n)unordered_map<char, int>freqDEFINE maxFreq = 0for (char task :tasks)freq[task]++maxFreq = max(maxFreq,freq[task])DEFINE maxCount = 0for (auto& p : freq)if (p.second ==maxFreq) maxCount++DEFINE time = (maxFreq - 1)* (n + 1) + maxCountRETURN max(time,(int)tasks.size())DEFINE main()vector<char> tasks = {'A','A', 'A', 'B', 'B', 'B'}cout <<leastInterval(tasks, 2) <<endlRETURN 0END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/21

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits