Counting Cows by Their Legs
Updated
Problem statement
A barn holds ants, cows and chickens. An ant has 6 legs, a cow has 4 legs and a chicken has 2 legs.
- There are exactly
antsPerChickentimes as many ants as chickens. - There are exactly
cowsPerAnttimes as many cows as ants. - There is at least one chicken, and every count is a whole number.
All the animals together have totalLegs legs. Return the number of cows, or -1 if no whole numbers of animals match the total.
Function
countCows(totalLegs: int, antsPerChicken: int, cowsPerAnt: int) → intExamples
Example 1
totalLegs = 570antsPerChicken = 2cowsPerAnt = 2return = 76This is the interview case. Each chicken comes with 2 ants and 2 × 2 = 4 cows. That group has 2 + 2 × 6 + 4 × 4 = 30 legs.
570 / 30 = 19, so there are 19 chickens, 38 ants and 76 cows.
Example 2
totalLegs = 12antsPerChicken = 1cowsPerAnt = 1return = 1One chicken, one ant and one cow have 2 + 6 + 4 = 12 legs, which is exactly the total. So there is 1 cow.
Example 3
totalLegs = 100antsPerChicken = 1cowsPerAnt = 1return = -1Every group of one chicken, one ant and one cow has 12 legs, and 100 is not a multiple of 12. No whole numbers fit, so the answer is -1.
Constraints
1 <= totalLegs <= 10^61 <= antsPerChicken <= 1001 <= cowsPerAnt <= 100
More Susquehanna International Group (SIG) Online Assessment questions
- Catch Fish with Reusable BaitsOA · Seen Nov 2026
- Bold Play to Reach a GoalOA · Seen Nov 2026
- Cats, Truth and Who Has MoreOA · Seen Nov 2026
- Enough Muffins for the LineOA · Seen Nov 2026
- Expected Consecutive Pairs Among Chosen NumbersOA · Seen Nov 2026
- Four-by-Four Boundary GridOA · Seen Nov 2026
- Frog Paths Without Long RunsOA · Seen Nov 2026
- Higher Roll, or Lose the SumOA · Seen Nov 2026
- Is a Popular Story Fake?OA · Seen Nov 2026
- Knights, Knaves and Their StatementsOA · Seen Nov 2026
Similar questions at other companies
- Count Valid Square PairsAuxoAI · OA
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Identification Number Check DigitAbridge · OAAbridge interview questions
- Count Substrings With Identical CharactersVirtu Financial · OAVirtu Financial interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Digit-Sum Sequence Join PointOpenAI · OAOpenAI interview questions
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
- Address on Same PageNVIDIA · OANVIDIA interview questions
- Preorder of a Special Binary TreeNielsen · OA