Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:21:09+00:00 2026-06-14T14:21:09+00:00

I came across an interview question asked at Google which I can not solve:

  • 0

I came across an interview question asked at Google which I can not solve:

There is a pile of N kg grains at an oasis located in a distance of D km to a town. The grains need to be transported by a camel cart whose initial location is at the oasis. The cart can carry C kg of grains at a time. The camel uses the grains as fuel while transporting them. It consumes F kg/km.

Write a function that computes the maximum amount of grains (X kg) that can be transported to the town.


I tried to use recursion but I couldn’t get much further without confusing myself.

Here’s what I have so far:

number of transports = N / C

fuel amount for distance D = D * F

X = N - ((number of transports) * 2 * (fuel amount for distance D))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:21:10+00:00Added an answer on June 14, 2026 at 2:21 pm

    Assuming that N, D, C and F are inputs, –

    float computeMaxGrains(float N, float D, float C, float F)
    {
        //Case when the cart can carry all grains at once
        if(N <= C)
        {
            float remainingGrains = N - D*F;
            if(remainingGrains >= 0)
            {
                return remainingGrains;
            }
            else
            {
                //out of fuel
                return 0;
            }
        }
    
        // Grains consumed per Km = (Total Number of Trips) * F
        // Total Number of Trips = 2*(N/C) + 1
        float grainsConsumedPerKm = (float) (2*(Math.floor(N/C)) + 1);
    
        // Remaining grains after Camels fuel = C*(N/C - 1)
        float remainingGrains = (float) (C*(Math.floor(N/C)));
    
        // Distance that the Camel is able to travel before the camel is 
        // 1 less round trip = 
        // (N - Remaining Grains) / grainsConsumedPerKm
        // From equation N - (grainsConsumedPerKm * distanceTravelled) = 
        // Remaining Grains
        float distanceTravelled = (N - remainingGrains) / grainsConsumedPerKm;
    
        if(distanceTravelled >= D)
        {
            return N - (D * grainsConsumedPerKm);
        }
    
        //Use recursion for every 1 less round trip
        return computeMaxGrains(remainingGrains, D-distanceTravelled, C, F);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across a interview question which i did not know the answer (
i came across an interview question which required me to simulate a car race
I came across this post , which reports the following interview question: Given two
I came across a interview question that asked to remove the repeated char from
i came across an interview question which asks while searching a value in an
Came across a problem which google searches could not help me with. I have
I came across this question on an interview questions thread. Here is the question:
I came across an interesting algorithm question in an interview. I gave my answer
I am preparing for my interview and came across this question: Consider that i
I am preparing for an interview and I came across these questions. Can some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.