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


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