Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
An airlines company has several planes of the same type.Each plane has a seating capacity of 24 X 3 rows and 8 seats in each row split has shown.

1 [a] [b] [c] [d] [e] [f] [g] [h]
2 [a] [b] [c] [d] [e] [f] [g] [h]
3 [a] [b] [c] [d] [e] [f] [g] [h]


If 4 people book - allocate 4 seats in middle row.Else 2 on the right and 2 on the left.

if 3 people book - id middle section is empty,allocate there continuously.Else go to next row middle section.

if 2 people book - allocate the edge seats.

if 1 person has booked - then allocate whatever free seat available.

eg-
input 4
output-1c 1d 1e 1f

input-3
output- 2c 2d 2e

input-2
output-1a 1b

This was asked to me in an interview. My logic doesn't work for all cases. Any help would be appreciated.
Posted
Updated 23-Aug-15 5:26am
v3
Comments
PIEBALDconsult 23-Aug-15 11:40am    
Just to be clear -- this plane has three decks with identical seating on all three?
Sergey Alexandrovich Kryukov 23-Aug-15 12:27pm    
What have you tried so far?
—SA
PIEBALDconsult 23-Aug-15 13:22pm    
See also, bin packing and knapsack problems.

It doesn't look that difficult: The requirements already define the algorithm (that uses independent allocation strategies).
At each input, based on the number of passengers, you just have to find the first fit, considering only available seats. What is your doubt about?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Aug-15 12:26pm    
5ed.
—SA
CPallini 23-Aug-15 13:10pm    
Thank you.
JavaScript
const seatingChart = [
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
];

function allocateSeats(numPeople) {
    //X denotes booked seat
    if (numPeople === 4) {
        // Allocate 4 seats in the middle row or edge seats.
        for (let index = 0;index < seatingChart.length;index++) {
            if (seatingChart[index][2] !== 'X' && seatingChart[index][3] !== 'X' && seatingChart[index][4] !== 'X' && seatingChart[index][5] !== 'X') {
                seatingChart[index][2] = 'X';
                seatingChart[index][3] = 'X';
                seatingChart[index][4] = 'X';
                seatingChart[index][5] = 'X';
                break;
            } else if (seatingChart[index][0] !== 'X' && seatingChart[index][1] !== 'X' && seatingChart[index][6] !== 'X' && seatingChart[index][7] !== 'X') {
                seatingChart[index][0] = 'X';
                seatingChart[index][1] = 'X';
                seatingChart[index][6] = 'X';
                seatingChart[index][7] = 'X';
                break;
            }
        }
    }
    if (numPeople === 3) {
        for (let index = 0;index < seatingChart.length;index++) {
            // If the middle section is empty, allocate there continuously.
            if (seatingChart[index][2] !== 'X' && seatingChart[index][3] !== 'X' && seatingChart[index][4] !== 'X') {
                seatingChart[index][2] = 'X';
                seatingChart[index][3] = 'X';
                seatingChart[index][4] = 'X';
                break;
            }
        }
    }

    if (numPeople === 2) {
        // Allocate the edge seats.
        for (let index = 0;index < seatingChart.length;index++) {
            if (seatingChart[index][0] !== 'X' && seatingChart[index][1] !== 'X') {
                seatingChart[index][0] = 'X';
                seatingChart[index][1] = 'X';
                break;
            } else if (seatingChart[index][6] !== 'X' && seatingChart[index][7] !== 'X') {
                seatingChart[index][6] = 'X';
                seatingChart[index][7] = 'X';
                break;
            }
        }
    }

    if (numPeople === 1) {
        seatingChart.filter(i => { debugger; });
        let isBreak = false;
        // Allocate whatever free seat is available.
        for (let i = 0;i < seatingChart.length;i++) {
            for (let j = 0;j < seatingChart[i].length;j++) {
                if (seatingChart[i][j] !== 'X') {
                    seatingChart[i][j] = 'X';
                    isBreak = true;
                    break;
                }
            }
            if (isBreak) {
                break;
            }
        }
    }

    console.log(seatingChart);
}


Here's a solution in JS.
 
Share this answer
 
Comments
Dave Kreskowiak 24-Aug-23 9:44am    
It's a really bad idea to do homework for someone, especially eight years later.
Richard Deeming 24-Aug-23 10:09am    
And in the wrong language to boot...
Dave Kreskowiak 24-Aug-23 10:17am    
Hehe. Some people are just so eager to write code, they never consider whether or not they should.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900