Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't develop logic

 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently in an interview I was asked to write code in Java for the following problem.


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
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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? :-) I don't mean to sound rude, just not sure where you're coming from on this (other than not being able to come up with some logic in the way of syntax to explain this problem). Honestly, it feels to me like there is either some information missing in that problem, or it's a very simple piece of logic all wrapped up in a rather terse "story", LOL Here is what I *think* is being asked:

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?
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying

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.
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies. This is what I have done so far.


Am I going in the right direction?
How to find if consecutive seats are empty? Check each seat?
Thanks
Thanks
[ August 02, 2005: Message edited by: Smita Chopra ]
 
Smita Chopra
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any other way than using 2 Dimensional Arrays, I find them extremly confusing to use.
Thanks
 
Parag Ballal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably, HashTable or HashMap
 
jin sun
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
drawing a picture might help you also to visualize.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic