Amazon Interview Question for SDE-3s


Country: United States




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

Not sure why the function take i, j, and k as parameters instead of length, width and floor to be more intuitive.

(int i, int j, int k) nextPreferredSlot = (-1, -1, -1);
        const int Length = 0; 
        const int Width = 1;
        const int Floor = 2;

	// parking Lot: true if occupied, false otherwise
        bool parkingLot[,,] = Initialise();// not implemented

        void Unpark(int i, int j, int k)
        {
            if (i >= parkingLot.GetLength(Length) || j >= parkingLot.GetLength(Width) || k >= parkingLot.GetLength(Floor))
                return; // 


            parkingLot[i, j, k] = false;

            if (k < nextPreferredSlot.k)
            {
                nextPreferredSlot = (i, j, k);
            }
            else if (k == nextPreferredSlot.k)
            {
                if (i < nextPreferredSlot.i)
                    nextPreferredSlot = (i, j, k);
                else if (i == nextPreferredSlot.i)
                {
                    if (j < nextPreferredSlot.j)
                        nextPreferredSlot = (i, j, k);
                }
            }
        }

        static void Park()
        {
            if (nextPreferredSlot.i == -1)
            {
                nextPreferredSlot = FindNextAvailableSlot((0,0,0));
                if (nextPreferredSlot.i == -1)
                    return; // No available slot;
            }

            parkingLot[nextPreferredSlot.i, nextPreferredSlot.j, nextPreferredSlot.k] = true;
            nextPreferredSlot = FindNextAvailableSlot(nextPreferredSlot);
        }

        static (int i, int j, int k) FindNextAvailableSlot((int i, int j, int k) currentSlot)
        {
            (int i, int j, int k) = currentSlot;
            for (; k < parkingLot.GetLength(Floor); k++)
            {
                for (; i < parkingLot.GetLength(Length); i++)
                {
                    for (; j < parkingLot.GetLength(Width); j++)
                    {
                        if (!parkingLot[i, j, k])
                            return (i, j, k);
                    }
                    j = 0;
                }
                i = 0;
            }
            return (-1, -1, -1); // Parking lot are full
        }

- Dung Nguyen April 14, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. If unpark was called, keep track of the parking space in priority queue where floor>length>width
2. Allocate new parking space

#include <iostream>
#include <queue>

using namespace std;
struct Location {
    size_t length = -1;
    size_t width = -1;
    size_t floor = -1;

    void Print()
    {
        if(IsOk()) { cout << "(" << length << ", " << width << ", " << floor << ")" << endl; }
    }

    bool operator>(const Location& other) const
    {
        // the order of comparison matters
        if(this->floor > other.floor) return true;
        if(this->floor < other.floor) return false;
        // same floor, compare length
        if(this->length > other.length) return true;
        if(this->length < other.length) return false;
        // same floor & length, just compare the width
        return this->width > other.width;
    }

    bool IsOk() const { return length != string::npos; }
};

; // Min heap
class ParkingLot
{
    size_t m_length = 0;
    size_t m_width = 0;
    size_t m_floor = 0;

    size_t m_nextLen = 0;
    size_t m_nextWidth = 0;
    size_t m_nextFloor = 0;
    bool m_isFull = false;
    priority_queue<Location, vector<Location>, greater<Location>> Q;

protected:
    /**
     * @brief find next parking space. return false if the parking lot is full
     */
    bool FindLocation(Location& loc)
    {
        loc = { string::npos, string::npos, string::npos };
        if(!Q.empty()) {
            // reuse parking spot
            loc = Q.top();
            Q.pop();
            return true;
        }
        if(m_isFull) { return false; }
        loc = { m_nextLen, m_nextWidth, m_nextFloor };

        // Allocate new spot
        if(m_nextWidth + 1 < m_width) {
            // progress the width, this keeps lowest available length / floor
            ++m_nextWidth;
        } else {
            // m_nextWidth is at its max, reset it back to 0
            m_nextWidth = 0;
            // attempt to increase the length first, since floor has highest priority
            if(m_nextLen + 1 < m_length) {
                ++m_nextLen;
            } else {
                m_nextLen = 0;
                if(m_nextFloor + 1 < m_floor) {
                    ++m_nextFloor;
                } else {
                    // full
                    m_isFull = true;
                    return true;
                }
            }
        }
        return true;
    }

public:
    ParkingLot(int length, int width, int floor)
        : m_length(length)
        , m_width(width)
        , m_floor(width)
    {
    }

    Location Park()
    {
        Location loc;
        if(!FindLocation(loc)) { cout << "parking lot is full" << endl; }
        return loc;
    }

    void Unpark(size_t len, size_t width, size_t floor) { Q.push({ len, width, floor }); }
};

ParkingLot P(3, 3, 3);

void Park()
{
    Location loc = P.Park();
    loc.Print();
}

void Unpark(int len, int width, int floor) { P.Unpark(len, width, floor); }

- PenChief May 01, 2019 | 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