27 November 2014

Quiz 55: Find outgoing traffic of chat server


We have a chat application.
Allowed operations are:
1> Adding a person to chat group, format +priyanka
2> Removing a person to chat group, format -sonali
3> Sending a message to chat group, format priyanka:Hi
Now, no traffic is sent to server for adding or removing person. But for every sent message, K bytes(length of message) for each person present in group are sent to server. Like if only Priyanka is there is a group, then Priyanka:Hello will send 5 bytes. If 2 persons are there in group, Priyanka:Hello will send 5x2=10 bytes.
Find total bytes sent to server.
Note: All input data is correct. A person cannot be added if it is already there in group. A person cannot be removed if he/she is not there is group. No two persons have same name.

Input Format: 

Instructions in each line:
Mohan:Hello how r u

Output Format: 

total bytes sent to server



Sample Input

priya:hey shikha, how r u
shikha:fine, thanks
shikha:lets add sonali
shikha:hi sonali
sonali:shikha, personal talks, remove priya
shikha:tell me now

Sample Output:



92(when 2 people in chat) + 135(when 3 people in chat) + 22(when again 2 people). total=249


my $input;
if($arr[0] eq '+'){$c++;next;}
if($arr[0] eq '-'){$c--;next;}
if($arr[$i] eq ':')
print $ans;


No use to maintain HASH for different names since we are just concerned about number of people in chat, not the names of person.

No comments:

Post a Comment