Published
Friday, December 16, 2005 11:35 AM
/*
Sorry for the delay putting up this puzzle but I was going to
post a different puzzle for this week but failed to get a solution
for it :)
When queuing for our food at the Christmas party, I noticed that several of us
had already overdone the alcoholic drinks. Whilst we waited, I wondered how
many drunk people were standing next to each other, and what was the longest
unbroken row of these inebriated people in the queue.
This, then is the puzzle. Below is a table that represents a queue of people. The
value in the Position field represents their position in the queue. The FirstName
Field holds the first name of the person. The IsDrunk
Field is 1 if the person is drunk and 0 otherwise. Using the table below,
construct a single query without using variables, other tables and ddl
that returns single rows resultset which holds the most number of drunk
people standing adjacent in a row within the queue. For the example data below
the results should be 4, as Simon, James, Tom and Lionel are standing next to each
other, drunk, in the queue.
Largest Number Of Drunk People In A Row
---------------------------------------
4
*/
SET NOCOUNT ON
DECLARE @Queue TABLE
(
Position INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
IsDrunk INT NOT NULL -- 0 is not Drunk 1 is drunk
)
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 1, 'Nick', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 2, 'John', 0
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 3, 'Simon', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 4, 'James', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 5, 'Tom', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 6, 'Lionel', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 7, 'Neil', 0
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 8, 'Andras', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 9, 'Richard', 1
INSERT INTO @Queue(Position, FirstName, IsDrunk)
SELECT 10, 'Helen', 0
SET NOCOUNT OFF