This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Can't develop logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't develop logic

Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
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

Joined: Aug 26, 2001
Posts: 123
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?


if you don't know, then ask. if you do know, then share. love is knowledge.
Smita Chopra
Ranch Hand

Joined: Jun 27, 2005
Posts: 45
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

Joined: Jun 27, 2005
Posts: 45
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

Joined: Aug 26, 2001
Posts: 123
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

Joined: Jun 27, 2005
Posts: 45
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

Joined: Jun 27, 2005
Posts: 45
Is there any other way than using 2 Dimensional Arrays, I find them extremly confusing to use.
Thanks
Parag Ballal
Greenhorn

Joined: Jul 28, 2005
Posts: 27
probably, HashTable or HashMap
jin sun
Ranch Hand

Joined: Feb 16, 2005
Posts: 30
drawing a picture might help you also to visualize.
 
jQuery in Action, 2nd edition
 
subject: Can't develop logic
 
Similar Threads
The question about LOCK,UNLOCK method
help with program
concurrent use of database
lock & unlock
How good is good enough?