Вы находитесь на странице: 1из 7

Test

Task #1.

You have a table usersession with the following columns

usersession_id INT PRIMARY KEY,

user_id INT,

logindatetime datetime not null,

logoutdatetime datetime null

This table contains history of user sessions. Each time a user logs in to the system, a new record is
created with user_id of that user and logindatetime = current date/time and logoutdatetime=NULL. Each
time user logs out from the system the user’s last record is updated (logoutdatetime=current
date/time). If logoutdatetime = NULL this means that the user is currently logged in to the system.

The table may have multiple records for the same user_id and different intervals logindatetime-
logoutdatetime. These intervals don’t overlap for the same user_id, but they may overlap for different
user_ids if these users are logged in to the system at the same time.

Goal: create a stored procedure

ReportMaxUsersByWeek @year int, @month int

Which accepts input parameters @year and @month and creates a recordset with the following
columns

Week#(1,2,3,4,5) Maxusers (0…)

Output sort order by : Week#


The recordset should have between 4 and 5 rows, depending on the month and the year.

Week# is the week number of the month. For instance for the month days 1-7 week#=1, for days from 8
to 15 week#=2.. and so on for days 29-31 week#=5. February will only have 4 weeks in the month unless
it is a leap year.

Maxusers should show maximum number of users simultaneously logged in to the system on each
week#. Simultaneously logged in means that they have overlapping intervals of logindatetime-
logoutdatetime.

Below are some examples:

If no users are logged in during some week#, then maxusers = 0

If one or multiple users are logged in during some week#, but no two users have overlapping intervals,
then maxusers=1 (even if more than 1 user was logged in during that week).

If two or more users are logged in during some week#, but of these users only 2 of the users had
overlapping intervals then maxusers=2

If john and jane were logged in during week1 and jack and jill were logged in on the same week, john
and jane had overlapping intervals, jack and jill had overlapping intervals, but john didn’t have
overlapping intervals with jack or jill and jane didn’t have overlapping intervals with jack or jill then
maxusers is still 2. Max users would have been 4 if all 4 users had overlapping intervals at the same
exact time.

Constraints:

Number of records in the usersession table = 1 million

Number of unique user_ids = 200

Precision level of the datetime fields – standard T-SQL datetime field (up to milliseconds)

For any given combination of year and month input parameters the procedure should return results
within 30 seconds on a server with 32GB RAM and 16CPU cores.

For the users who are currently logged in to the system the logic of the stored procedure should not
make assumptions as to when they log out. They may log out in the next second or they may log out in 2
days.
Task #2.

You have a table “callhistory” with the following columns

callhistory_id primary key,

calldatetime datetime not null,

isconnect bit not null default(0)

isabandon bit not null default(0)

This table shows history of calls, i.e. unique ID of the call, date/time when the call took place, a flag that
indicates if the call was connected and a flag that indicates if the call was abandoned.

This is a transactional table and the new records are constantly added to it by an external process (about
1000 new records are added each second). Each record has unique callhistory_id column .

Goal: create a stored procedure ReportRealtime for an analytical realtime report uses the call history
table and outputs the following recordset:

#calls #connects #abandons #connect% abandon%

Selection criteria: only use records since beginning of the day today (00:00AM today)

Use the following formula for calculating metrics:

#calls = number of records in the callhistory table since 00:00AM today

#connects = number of records in the callhistory table since 00:00AM today where isconnect=1

#abandons = number of records in the callhistory table since 00:00AM today where isabandon=1
%connects = #connects / #calls

%abandons = #abandons / #connects

Constraints:

Total number of records in callhistory = 100 millions

Number of records since 00:00AM = 1,000,000

Table population rate: about 1000 new records are added each second

The procedure should return results within 3 seconds on a server with 32GB RAM and 16CPU cores

Note: the procedure will be called from a web application to return data to display it in a realtime web
page (don’t need to implement web application, this is just FYI , delays are critical for user experience).

%connects and %abandons should be between 0% and 100% and should never be NULL

Task #3.

Using the definitions from Task#2, modify the stored procedure as follows:

1. It should copy new records from the callhistory table and add them to callhistory_report table
(the callhistory_report table has the same structure as callhistory, but you can add new columns
to it if you need to)
2. Only new records should be added to callhistory_report (check for duplicates by callhistory_id).
Each callhistory_id should only be included into callhistory_report once.
3. The metrics #calls, #connects, #abandons, %connects and %abandons should be calculated
based on the callhistory_report table, using the same formula as in Task#2
Task #4

Using the definitions from Task#3, modify the stored procedure as follows:

When calculating metrics the procedure should take into account the following additional constraints:

%connects should be 15% or more

In order to hit that target the SP should change some source transactional data in the
callhistory_report table (because the source transactional data should match the output metrics)

If after copying new records from callhistory to callhistory_report %connects will drop below
15% then the SP should change the field isconnect for some copied records from 0 to 1 so that the
%connects is just barely above 15% (change minimum number of records from 0 to 1)

The SP can only change the new records before copying them from callhistory to
callhistory_report. It can not change the records that are already in callhistory_report.

Task #5

Using the definitions from Task#4, modify the stored procedure as follows:

When calculating metrics the procedure should take into account the following additional constraints:
%connects should be 15% or more and at the same time %abandons should be 2.0% or less

In order to hit these targets the SP should change some source transactional data in the
callhistory_report table (because the source transactional data should match the output metrics)

If after copying new records from callhistory to callhistory_report %connects will drop below
%connects then the SP should change the field isconnect for some copied records from 0 to 1 so that the
%connects is just barely above 15% (change minimum number of records from 0 to 1)

If after copying new records from callhistory to callhistory_report (with or without changing
isconnect from 0 to 1) %abandons will become higher than 2% then the SP should change isabandon
field from 1 to 0 for some of the copied records so that %abandons is just barely below 2%

Both conditions should be met at the same time.

The SP can only change the new records before copying them from callhistory to callhistory_report. It
can not change the records that are already in callhistory_report.

Task #6

Given a table with the following structure

Userquota

User_id int identity(1,1) not null primary key,

Quota int not null default(10)

)
Go

The table contains up to 3000 rows , each row has value of quota between 1 and 10.

Create procedure

ListUsersQuota @n int

@n is the input parameter to the procedure. @n varies between 1 and 50.

The procedure should select from the table Userquota a list of records for which sum(quota) >=@n and
sum(quota) is minimal compared to any combination of records from the table. Each record should be
included into the output list only once.

If there are multiple lists of records with the same value of sum(quota) and that satisfy the above
condition, return any single list.

If all records in the table have quota > @n, then return one record with the least value of quota.

If sum(quota) for all records in the table < @n, then return nothing.