Log Analysis and Processing in C++ for Social Networking Apps

Introduction

Welcome to our new coding practice lesson! We have an interesting problem in this unit that centers around data from a social networking app. The challenge involves processing logs from this app and extracting useful information from them. This task will leverage your skills in string manipulation, working with timestamps, and task subdivision. Let's get started!

Task Statement

Imagine a social networking application that allows users to form groups. Each group has a unique ID ranging from 1 up to n, the total number of groups. Interestingly, the app keeps track of when a group is created and deleted, logging all these actions in a string.

The task before us is to create a C++ function named analyzeLogs(). This function will take as input a string of logs and output a vector of pairs representing the groups with the longest lifetime. Each pair contains two items: the group ID and the group's lifetime. By 'lifetime,' we mean the duration from when the group was created until its deletion. If a group has been created and deleted multiple times, the lifetime is the total sum of those durations. If multiple groups have the same longest lifetime, the function should return all such groups in ascending order of their IDs.

For example, if we have a log string as follows: "1 create 09:00, 2 create 10:00, 1 delete 12:00, 3 create 13:00, 2 delete 15:00, 3 delete 16:00", the function will return: {{2, "05:00"}}.

Solution Building: Step 1

Firstly, we will split the input string into individual operations. In C++, string manipulation can be handled using functions from the string library and streams from the sstream library.

C++
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <algorithm>

std::vector<std::pair<int, std::string>> analyzeLogs(std::string logs) {
    std::vector<std::string> logList;
    std::stringstream ss(logs);
    std::string log;
    
    // Break down the log string into individual logs by splitting
    while (std::getline(ss, log, ',')) {
        logList.push_back(log);
    }

Solution Building: Step 2

Next, we delve deeper into the logs. For each logged group operation in the string, we need to parse its components. These include the group ID, the type of operation (create or delete), and the time of action.

C++
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <algorithm>

std::vector<std::pair<int, std::string>> analyzeLogs(std::string logs) {
    std::vector<std::string> logList;
    std::stringstream ss(logs);
    std::string log;
    
    while (std::getline(ss, log, ',')) {
        logList.push_back(log);
    }
    
    std::unordered_map<int, std::pair<int, int>> timeDict; // Dictionary to record the creation moment for each group in minutes
    std::map<int, int> lifeDict; // Dictionary to record the lifetime for each group in minutes

    for (const auto &log : logList) {
        std::stringstream logStream(log);
        int groupId;
        std::string action, time;
        logStream >> groupId >> action >> time;
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal