In a train compartment there are 67 seats...13 rows of 5 (3 by2) seats

and the last row has 2 sets. A person cannot book more than 5 seats. If a

person is booking then it should be such that maximum people of the group get

consecutive seats.

I can't develop logic for this and which datastructure should I use to store seats which have been assigned and still empty.

Please give me pointers in this regard.

Thanks

If personA asks for tickets, then

...Ask personA for number of tickets being requested

...Get number of tickets

...If number of tickets is greater than five then

......Reject ticket request

...Else

......Loop through each row

.........If number of available seats is >= than request then

............Fill seats in that row equal to request

...............Set number of seats available minus request

.........End If

......End Loop

...End If

End If

Am I understanding the problem correctly?

if you don't know, then ask. if you do know, then share. love is knowledge.

Originally posted by Thomas Whalen:

I'm only a mere beginner, but...why are you getting questions in a job interview (I thought only highly qualified Java programmers got interviews anymore) and asking them in a beginner forum? :-)

That is becaue I am a beginner myself. Graduated 15 days ago.

Originally posted by Thomas Whalen:

If personA asks for tickets, then

...Ask personA for number of tickets being requested

...Get number of tickets

...If number of tickets is greater than five then

......Reject ticket request

...Else

......Loop through each row

.........If number of available seats is >= than request then

............Fill seats in that row equal to request

...............Set number of seats available minus request

.........End If

......End Loop

...End If

End If

Am I understanding the problem correctly?

Thanks for you input.

But what if second..third...tenth person comes and asks for a ticket. You'll have to see which all seats have been filled. Of the vacant seats you'll have to look for consecutive seats. If consecutive seats are not available you'll have to assign them dispersed seats and update your list of filled seats.

Thanks

Originally posted by Smita Chopra:

Thanks for you input.

But what if second..third...tenth person comes and asks for a ticket. You'll have to see which all seats have been filled. Of the vacant seats you'll have to look for consecutive seats. If consecutive seats are not available you'll have to assign them dispersed seats and update your list of filled seats.

Thanks

In that case, you take what I've already done and then while you're looping through each row, you check for consecutive numbers like this:

If NumberOfTicketsRequested <=5 then

...Loop through row

......If seat[row][seat] is vacant then

.........If seat[row][seat - 1] is vacant then

............counterConsecutive++

.........End if

......End if

...End loop

If counterConsecutive >= NumberOfTicketsRequested then

...// find a way to identify the first seat found in a string of consecutive vacant seats found

End If

if you don't know, then ask. if you do know, then share. love is knowledge.