Home » Interview coding problems/challenges

Where is my seat?

Here, we are going to learn about the implementation of a coding problem where is my seat? This is competitive coding type problem.
Submitted by Debasis Jana, on March 22, 2019

Problem statement:

You and your friend are deciding to go for a tour by train. But before booking the ticket of the train, your friend asks you to book a ticket in such a way that both of you sit face to face.

Seating decoration is known to you (given in the image) and you as a VIP customer are allowed to book seat number according to your choice.

ICP - Where is my seat?

After booking the ticket accordingly, your friend asks you, your seat number so that he can check which is his seat number and seat position (WS/MS/AS)?

Example:

    Input:
    You have given your seat number N

    Output:
    Print your friend's seat number and seating position
    WS: For Window Seat
    MS: For Middle Seat
    AS: For Aisle Seat

    -----------------
    Sample Input:
    57
    23

    Sample Output:
    52 AS
    14 MS

Note: Before going to solution, we recommend you to try it by yourself first...

Hint: Starting from bottom to top (1 to 6) sees the difference of opposite seat number, [11,9,7,5,3,1]

Algorithm:

    1. Take input N
    2. Switch (N%12)
        2.1     Case 1: print N+11 and "WS"
        2.2     Case 2: print N+9 and "MS"
        2.3     Case 3: print N+7 and "AS"
        2.4     Case 4: print N+5 and "AS"
        2.5     Case 5: print N+3 and "MS"
        2.6     Case 6: print N+1 and "WS"
        2.7     Case 7: print N-1 and "WS"
        2.8     Case 8: print N-3 and "MS"
        2.9     Case 9: print N-5 and "AS"
        2.10    Case 10: print N-7 and "AS"
        2.11    Case 11: print N-9 and "MS"
        2.12    Default : print N-11 and "WS" 
    3. END OF PROGRAM

Explanation:

Here we are finding mod of N (entered seat number) by 12 because there are 12 seats in a single cabin of the train. Now, if we divide it by 12 we will be able to identify each seat individually by its number, first 6 (1 to 6) are at the left side and next 6 (7 to 12) are at the right side.

For example, if N=52 then, N%12=4, so 52nd seat will get a unique number in that particular cabin and it is at the left side.

Now, by seeing the given picture we can say that difference between seat number 1 and 12 is 11, 2 and 11 is 9 and so on, means its decreasing by two, starting from 11.

So, for seat number 4 difference will be 5, that's why the opposite seat of 52 is 52+5=57.

Similarly, for N=23, the mod value is 11, and the seat difference of the opposite seat will be 9. But in this case, we have to deduct 9 from N because N is at the right side. So, ans: is 23-9=14.


C++ implementation:

#include <bits/stdc++.h>
using namespace std;

int main(){
	int N;
	//taking your seat number
	cin>>N; 	
	
	switch(N%12)
	{			
		// Finding modulous of 12 because there are 12 seats in a cabin
		// First 6 seats are at left side and next 6 seats (7 to 12) 
		//are at right side of a cabin
		case 1: 
			cout<<N+11<<" WS";
			break;
		//if modulous is 2 then we can see that seat number is 2 
		case 2: 							
			//so here the opposite seat is at right side and 
			//seat difference is 9		
			cout<<N+9<<" MS";			
			break;		
		case 3: 
			// here seat difference is 7 and opposite 
			//side is at right side
			cout<<N+7<<" AS";			
			break;
		case 4: 
			cout<<N+5<<" AS";
			break;
		case 5: 
			cout<<N+3<<" MS";
			break;
		case 6: 
			cout<<N+1<<" WS";
			break;
		// as said above seat number from 7 to 12 are at right side
		// so opposite seat will be at left side and seat difference is 1
		case 7: 
			cout<<N-1<<" WS";
			break;
		case 8: 
			cout<<N-3<<" MS";		
			break;
		case 9: 
			cout<<N-5<<" AS";
			break;
		case 10: 
			cout<<N-7<<" AS";
			break;
		case 11:
			cout<<N-9<<" MS";
			break;
		default:
			cout<<N-11<<" WS";
			break; 
	}
	return 0;
}

Output

ICP | where is seat output 1

ICP | where is seat output 2






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.