All Tracks Problem

Canteen Logs
Tag(s):

Medium

Problem
Editorial
Analytics

Fredo and his friends regularly visit their college canteen. As with any group, on a day , one of the friends pays the canteen bill of all the friends. You are given a table of logs which shows the entries of transactions between friends. The table consists of three fields as described below:
1. P1: Name of the person who pays the bill.
2. P2: Name of the person whose bill is paid by P1.
3. amount: Amount paid by P1 for P2.

You have to summarise the transaction between all pairs of friends. See the sample input and output for explanation.

Input Format:

Table : logs

Field Type
P1 text
P2 text
amount int

Output Format:

Field Type
P1 text
P2 text
NetAmount int

Sample Input:

Sample logs Table:

P1 P2 amount
Fredo Zeus 81
Fredo John 59
Zeus Fredo 81
Zeus John 16
John Fredo 27
John Zeus 83
Fredo Zeus 27
Fredo John 17

Sample Output:

P1 P2 NetAmount
Fredo John 49
Fredo Zeus 27
John Zeus 67

Explanation:

Here Fredo lends John 59+17=76 and John lends Fredo 27. So, in all John owes Fredo 49 units.
Similarly, Fredo lends Zeus 81+27=108 and Zeus lends Fredo 81. So, in all Zeus owes Fredo 17 units.
Similarly, Zeus owes John 67 units.

Note:

Only direct transactions are to be covered in the output table.
Only those entries should come in the output table which have NetAmount greater than 0.
The output table should be ordered by P1 in ascending order and then by P2 in ascending order.
It is guaranteed that the input table will contain all ordered pairs of friends atleast once.

Time Limit: 5 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: MySQL, PostgreSQL, MSSQL, Oracle Database

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

Notifications
View All Notifications