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 6808765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:59:46+00:00 2026-05-26T19:59:46+00:00

I got this question in an interview and I was not able to solve

  • 0

I got this question in an interview and I was not able to solve it.

You have a circular road, with N number of gas stations. You know the
ammount of gas that each station has. You know the ammount of gas you
need to GO from one station to the next one. Your car starts with 0
gas. The question is: Create an algorithm, to know from which gas
station you must start driving to COMPLETE the circular PATH. It does
not specify that you must visit all stations. You can only drive
clockwise.

I had to do it in c#

The only code I started is with a GasStation entity

class GasStation
  int gasAtStation;
  int gasToMoveToNextStationNeeded;
  string nameOfGasStation;


GasTation wheretoStart(List<GasStation> list)
{

}

I did it this way:

static void Main(string[] args)
        {
            int[] gasOnStation = {1, 2, 0, 4};
            int[] gasDrivingCostTonNextStation = {1, 1,2, 1};

            FindStartingPoint(gasOnStation, gasDrivingCostTonNextStation);

        }

        static void FindStartingPoint(int[] gasOnStation, int[] gasDrivingCosts)
        {
            // Assume gasOnStation.length == gasDrivingCosts.length
            int n = gasOnStation.Length;
            int[] gasEndValues = new int[n];
            int gasValue = 0;
            for (int i = 0; i < n; i++)
            {
                gasEndValues[i] = gasValue;
                gasValue += gasOnStation[i];
                gasValue -= gasDrivingCosts[i];
            }

            if (gasValue < 0)
            {
                Console.WriteLine("Instance does not have a solution");
                Console.ReadLine();
            }
            else
            {
                // Find the minimum in gasEndValues:
                int minI = 0;
                int minEndValue = gasEndValues[0];
                for (int i = 1; i < n; i++)
                {
                    if (gasEndValues[i] < minEndValue)
                    {
                        minI = i;
                        minEndValue = gasEndValues[i];
                    }
                }

                Console.WriteLine("Start at station: " + minI);
                Console.ReadLine();
            }
        }

Thanks

  • 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-05-26T19:59:46+00:00Added an answer on May 26, 2026 at 7:59 pm

    One easy way of solving this is using a brute force method. i.e. try every posibility and throw out ones that don’t work.

    i.e. Start at each gas station in turn (repeat below for each starting station).

    • Have a varible that defines current gas level.
    • Loop through each gas station, in a clockwise order.
      1. Fill up your gas (increment gas by gas station amount).
      2. Check you can move to the next station (gas >= gasToMoveToNextStationNeeded)
        • If not, this isn’t a solution, so move to the next starting location.
        • If so, subtract that amount of gas used, then keep going until you reach the start again.
      3. If you get back to the starting gas station you have an answer.

    Edit As per @Vash’s answer, As an improvement when deciding where to start, discount stations that don’t have enough gas themselves to get to the next station and working through starting stations in order of amount of gas (descending).


    Note, this assumes we visit all gas stations. Will need refinement for skipping gas stations if you need an optimal solution (question doesn’t specify this).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have got this question to solve from one my friends. i have searched
I got asked this question in a interview. I clearly know what a decorator
I just got this question on a SE position interview, and I'm not quite
I just got this question on an interview and had no idea how to
I got this in an interview question -- the question was more about what
The responses I've got to this question have solved the problem I had in
I got this question in an interview with amazon. I was asked to perform
My girlfriend got this question in an interview, and I liked it so much
I got this question in a previous interview and couldnt do it , any
I got this question in Interview for post of SSE in ASP.NET. the interview

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.