Hire K Contractors Picking the Cheapest From Either End of a Queue

Last updated: November 29, 2026

Quick Overview

Contractor quotes are in a line. In each of k rounds, look at the first c and last c remaining quotes, hire the cheapest among them (ties by lower index), and remove that contractor. Return the total cost.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Medium

0

0

0 solved


Contractor quotes are in a line. In each of k rounds, look at the first c and last c remaining quotes, hire the cheapest among them (ties by lower index), and remove that contractor. Return the total cost.

An online-assessment style question in a vendor-sourcing frame, the kind Google uses to test two heaps over two windows. The interviewer checks that the windows never double-count when they meet in the middle.

What the Interviewer Expects
  • Maintains a min-heap for the front window and one for the back
  • Refills each heap from a moving pointer
  • Prevents overlap when pointers cross
  • Breaks ties by preferring the front
  • States O((c + k) log c)
Key Topics to Cover
Min-Heap
Two Heaps
Two Pointers
Simulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the window size differs per side?
  • Return which indices were hired
  • What if quotes update while hiring?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list costs, integers k and c (candidates per side).
  • Each round consider the first c and last c of the remaining list; if fewer than 2c remain, consider all.
  • Hire the minimum cost, t...
High-Level Design

Keep two min-heaps: front for the leftmost unconsidered candidates, back for the rightmost, plus pointers lo and hi marking the next indices to pull in. Fill front with up to c from the left...


Submit Your Answer
Markdown supported