Programming Challenge Description: IBM is implementing a streaming analytics ser
ID: 3718223 • Letter: P
Question
Programming Challenge Description:
IBM is implementing a streaming analytics service for an online retailer to provide data on the best time to sell specific products. To accomplish this goal, the service needs a very fast way to calculate a number of basic formulas for a series of dates, quantity and product id.
Assumptions:
In production, the list of data could be in the hundreds of thousands of dates with quantities and product ids.
The service will be called frequently as the user explores different data models.
The data may not be normalized. There could be multiple lines with the same date and product id. In these cases you should sum all the quantity on input and work on that.
Product ids are case sensitive.
Input:
Each line will contain a comma separated list of text date (yyyy-mm-dd), quantity, product id. For example:
2017-06-02,5,Apples
2017-06-02,2,pears
2017-06-03,3,pineapples
Output:
For each date create a line of this format: date, total items sold for date, average quantity of all items (to 2 decimal places), count of unique parts sold. For example:
2017-06-02,7,3.50,2
2017-06-03,3,3.00,1
Explanation / Answer
Solution:
Note: Language is not mentioned, This is implemented in C++.
code:
#include<bits/stdc++.h>
using namespace std;
struct temp
{
set<string> s;
long long int tot=0;
};
int main()
{
string dat,pro;
long long int quant, i;
map<string, temp> mp;
while(!cin.eof())
{
cin>>dat>>",">>quant>>",">>pro;
mp[dat].s.insert(pro);
mp[dat].tot+=quant;
}
for(std::map<string,temp>::iterator it=mp.begin();it!=mp.end();it++)
{
long long int siz= (*it).second.s.size();
long double avg = (*it).second.tot / siz ;
cout<<(*it).first<<","<<(*it).second.tot<<","<<std::setprecision(2)<<avg<<","<<siz<<endl;
}
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.