Minimum Cost to Select People for Skill Quotas
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a Visa OA with a constraint satisfaction problem disguised as hiring. The twist: people have skills (one, both, or none), and you need to find minimum cost subsets that hit quotas for Skill 1 and Skill 2 independently. For each quota K from 1 to n, compute the cheapest way to select at least K people with Skill 1 AND at least K people with Skill 2. People with both skills count toward both. This is a greedy + sorting problem wrapped in a multi-constraint envelope. If you hit a wall on the OA, StealthCoder can spot the pattern and walk you through the cost computation in real time.
The problem
There are n people. Person i has hiring cost costs[i] and may have Skill 1, Skill 2, both skills, or neither skill. For every K from 1 to n, compute the minimum total hiring cost needed so that the selected subset contains at least K people with Skill 1 and at least K people with Skill 2. A person who has both skills contributes to both counts. Return an array ans of length n where ans[K - 1] is the minimum cost for that quota, or -1 if it is impossible. Function Description Complete the function minimumCostForSkillQuotas in the editor below. minimumCostForSkillQuotas has the following parameters: Returns long[]: the minimum cost for every quota size K = 1..n. For K = 1, hiring only the third person is enough because that person has both skills. For K = 2, the cheapest feasible subset costs 10. Larger quotas are impossible. For K = 1, hiring the third person alone is sufficient. For K = 2, all three people must be hired.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The key insight is greedy ordering. Sort people by cost, then iterate through all possible subsets in increasing cost order. For each subset, count how many have Skill 1 and how many have Skill 2. When a subset first hits quota K for both skills, record that cost as ans[K - 1]. A person with both skills is gold because they tick both counters. Common trap: brute-forcing all 2^n subsets will timeout. Instead, use a cost-ordered greedy traversal. You don't need all combinations; you just need the cheapest path to each quota pair. The constraint satisfaction here is what makes it tricky: you're not minimizing a single objective, you're solving n separate constraint problems (one per K). StealthCoder can help you verify the subset logic if your first pass times out or gives wrong answers on the examples.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Minimum Cost to Select People for Skill Quotas cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Visa's OA.
Visa reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Cost to Select People for Skill Quotas FAQ
Is this a dynamic programming problem?+
Not in the classic sense. You don't need DP states. The pattern is greedy: sort by cost, then iterate subsets in cost order, tracking skill counts. Once you hit quota K, record it. Move to the next quota. This is much faster than DP and cleaner to code.
How do I handle people with both skills?+
They increment both skill counters simultaneously. So if you're chasing quota K and a person has both skills, they bring you one step closer on two fronts. This is why people with both skills are often part of optimal subsets.
Can I return -1 for impossible quotas?+
Yes. If the total count of people with Skill 1 is less than K, or Skill 2 is less than K, it's impossible. Return -1 for ans[K - 1]. Build a quick count at the start.
What's the expected time complexity?+
If you sort by cost and iterate smartly, you can do this in O(n^2) or O(n^2 log n). Brute force subsets is O(2^n), which will fail. The key is not generating all subsets, just traversing cost order and tracking quotas.
How do I verify my answer on the Visa examples?+
Manually compute small cases. For K=1, find the cheapest person with both skills or the cheapest combo of one-skill people. For K=2, enumerate affordable subsets and check if they hit both quotas. Walk through the problem statement examples step by step before you code.