Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Gloves Pair Selection Math

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Gloves Pair Selection Math. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

An IFS Cloud warehouse stores gloves in several colors. Gloves are picked one at a time in complete darkness, and the picking order is unknown. Given the available quantity for each color, calculate the minimum number of gloves that must be picked to guarantee at least one matching pair of the same color.

The result must be guaranteed for the worst possible picking order. A pair consists of two gloves with the same color. You do not need to return the colors picked or simulate a particular order.

Formal Specification

Implement gloves_to_guarantee_pair(glove_counts), where glove_counts is a dictionary mapping a color name to its non-negative number of gloves. Return an integer representing the minimum number of picks that guarantees a matching pair. The input is guaranteed to contain enough gloves for a pair.

Constraints

  • 1 <= len(glove_counts) <= 10^5
  • 0 <= glove_counts[color] <= 10^9
  • At least one color has a quantity of at least 2
  • Color names are strings
  • The input contains enough gloves to guarantee a pair

Function Signature

def gloves_to_guarantee_pair(glove_counts):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output