Microsoft Interview Question


Country: United States
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

The brute force is exponential; however, the solution below has TC = O(n^2) and SC = O(n).

import java.util.Arrays;
import java.util.Collections;

class GasStation implements Comparable<GasStation> {
    double distance;
    double fuelGallons;

    public GasStation(double distance, double fuelGallons) {
        this.distance = distance;
        this.fuelGallons = fuelGallons;
    }

    @Override
    public int compareTo(GasStation o) {
        return Double.compare(this.distance, o.distance);
    }
}

public class GasStationStops {
    public static final int INSUFFICIENT_CREDIT = -1;
    public static final int NO_EXACT_MATCH = -2;

    static int find(GasStation[] gasStations, double availableGallons, double maxDistance) {
        if (gasStations == null || gasStations.length == 0) {
            return INSUFFICIENT_CREDIT;
        }
        Arrays.sort(gasStations, Collections.reverseOrder());
        if (maxDistance < gasStations[0].distance) {
            return INSUFFICIENT_CREDIT;
        }

        double[] requiredGallons = new double[gasStations.length + 1];
        double currentDistance = 0.0;
        int reGlIndx = 1;
        for (int gasStIndx = gasStations.length - 1; gasStIndx >= 0; gasStIndx--) {
            GasStation gasStation = gasStations[gasStIndx];
            updateRequiredGallons(requiredGallons, reGlIndx, gasStation.distance - currentDistance, gasStation.fuelGallons);
            currentDistance = gasStation.distance;
            reGlIndx++;
        }
        double distanceToFirstLocation = maxDistance - gasStations[0].distance;
        System.out.print(Arrays.toString(requiredGallons));
        return binarySearch(requiredGallons, availableGallons - distanceToFirstLocation, 0, requiredGallons.length - 1);
    }

    private static void updateRequiredGallons(double[] requiredGallons, int reGlIndx, double delta, double moreGallons) {
        for (int i = 0; i < reGlIndx; i++) {
            requiredGallons[i] += delta;
        }
        requiredGallons[reGlIndx] = requiredGallons[reGlIndx - 1] - moreGallons;
        for (int j = reGlIndx - 1; j > 0; j--) {
            requiredGallons[j] = Math.min(requiredGallons[j - 1] - moreGallons, requiredGallons[j]);
        }
    }

    private static int binarySearch(double[] a, double target, int i, int j) {
        if (i < 0 || i >= a.length || j < 0 || j >= a.length || i > j)
            return NO_EXACT_MATCH;

        int m = (i + j) / 2;
        if (a[m] == target)
            return m;

        int p;
        if (target < a[m]) {
            p = binarySearch(a, target, m + 1, j);
            if (p == NO_EXACT_MATCH) {
                return m < a.length - 1 ? m + 1 : INSUFFICIENT_CREDIT;
            }
        } else {
            p = binarySearch(a, target, i, m - 1);
            if (p == NO_EXACT_MATCH) {
                return m;
            }
        }
        return p;
    }

    public static void main(String[] args) {
        print(find(null, 10, 20));
        print(find(new GasStation[]{}, 10, 20));
        print(find(new GasStation[]{
                new GasStation(16, 3),
                new GasStation(10, 7),
                new GasStation(14, 11),
                new GasStation(11, 5),
                new GasStation(7, 6)}, 10, 20));
        print(find(new GasStation[]{
                new GasStation(15, 4),
                new GasStation(10, 7),
                new GasStation(5, 3)}, 10, 20));
        print(find(new GasStation[]{
                new GasStation(10, 7),
                new GasStation(15, 4),
                new GasStation(5, 3)}, 10, 20));
        print(find(new GasStation[]{
                new GasStation(10, 7),
                new GasStation(15, 4),
                new GasStation(5, 3)}, 6, 20));
        print(find(new GasStation[]{
                new GasStation(10, 7),
                new GasStation(15, 4),
                new GasStation(5, 3)}, 9, 20));
        print(find(new GasStation[]{
                new GasStation(10, 7),
                new GasStation(15, 4),
                new GasStation(5, 3)}, 12, 20));
        print(find(new GasStation[]{
                new GasStation(10, 7),
                new GasStation(15, 4),
                new GasStation(5, 3)}, 13, 20));
        print(find(new GasStation[]{
                new GasStation(10, 6),
                new GasStation(15, 3),
                new GasStation(5, 1)}, 10, 20));
        print(find(new GasStation[]{
                new GasStation(10, 1),
                new GasStation(15, 1),
                new GasStation(5, 1)}, 10, 20));
    }

    static void print(int x) {
        if (x == INSUFFICIENT_CREDIT) {
            System.out.println("Min # Stops:INSUFFICIENT INITIAL GALLONS");
        } else {
            System.out.println(", Min # Stops:" + x);
        }
    }
}

- George Curious March 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you have :
eg: gas = 10 , distance = 20
gasStation[] = {{16,3}, {10, 7}, {14, 11},{11, 5}, {7, 6}}

Then how can you opt for 14,11? because the initial gas itself is 10, Hence the only option for you is either , 10,7 or 7,6

I can think of Backtrack as one of the way to solve this

- Ashutosh April 25, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More