FastPrepMaximum Bananas Delivered by a Camel

Maximum Bananas Delivered by a Camel

StackAdapt logoStackAdapt● HardFULLTIMEONSITE INTERVIEW
Learn

Problem statement

A camel starts with bananas bananas and must transport as many as possible across distance units. It can carry at most capacity bananas at once and consumes one banana per distance unit traveled, including return trips. Bananas may be cached at any real-valued position.

Return the maximum amount that reaches the destination, formatted with exactly six decimal places.

Function

maxDeliveredBananas(bananas: int, distance: int, capacity: int) → String

Examples

Example 1

bananas = 3000distance = 1000capacity = 1000return = "533.333333"

Three-load and two-load shuttle phases precede the final one-way trip.

Example 2

bananas = 1000distance = 100capacity = 1000return = "900.000000"

The entire load fits, so only the direct trip consumes bananas.

Constraints

  • 1 <= bananas, capacity <= 10^9.
  • 0 <= distance <= 10^9.

More StackAdapt problems

See StackAdapt hiring insights
public String maxDeliveredBananas(int bananas, int distance, int capacity) {
  // write your code here
}
bananas3000
distance1000
capacity1000
expected"533.333333"
Checking account…