Transforming Social Network Logs into Insights with TypeScript

Introduction

Welcome to our new coding practice lesson! We have an intriguing problem centered around data from a social networking app. The challenge involves processing logs from this app and extracting valuable 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 to n, where n is the total number of groups. The app keeps track of group creation and deletion events, logging all these actions in a string.

The task before us is to create a TypeScript function named analyzeLogs. This function will take as input a string of logs and output an array of strings representing the groups with the longest lifetime. Each string in the array contains two items separated by a space: 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. 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"].

If multiple groups have the same longest lifetime, the function should return all such groups in ascending order of their IDs. Let's say we have the log string "1 create 08:00, 2 create 09:00, 1 delete 10:00, 2 delete 11:00, 3 create 12:00, 3 delete 13:00". In this case, both group 1 and group 2 have the same lifetime of 02:00. The function should return ["1 02:00", "2 02:00"] since both have the longest lifetime, and group 1 comes before group 2.

Step Overview

To tackle this problem, we will take the following steps:

  • Split the Log Strings: Divide the input string into individual log entries based on a delimiter.
  • Parse Log Components: For each log entry, identify the group ID, action type, and timestamp.
  • Record and Calculate Lifetimes: Track creation times and compute lifetimes whenever a group is deleted.
  • Identify Longest Lifetimes: Compare the lifetimes of all groups to determine the ones with the longest.

Splitting the Log Strings

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