PhonePe

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-03-05Added By Anonymous
Source: Link

Company: PhonePe
Role: software engineer 1, software engineer 2
Interview Stage: Machine Coding Round
Location: unknown
Year: unknown

Original Post Content:

Level 1 - Current level

Event-Aggregator
Machine Coding Round

All kinds of software services publish events. Event can be a transaction done by a user or it can be a user login. An "event aggregator" refers to a system or component within a software application that collects and combines data from various events based on predefined criteria.

This type of event aggregator is commonly used in analytics, monitoring, or reporting systems where data from multiple events needs to be aggregated and analyzed for insights or decision-making purposes.

Let’s implement an Event aggregator with the following set of rules:

  • Implement an event aggregator which can process “n” number of events and “m” types of events.
  • Implement an event queue to queue the incoming events. The aggregator will process these events in the order that they are received.
  • The queue should be in memory and should not require access to the file system.
  • The aggregator should aggregate events in real time, i.e. the aggregate value should be calculated/ updated by the aggregator as soon as it receives the event.
  • The aggregator should provide event filtering capabilities. (Process only those events that satisfy a predefined condition).
  • The aggregator should provide a time-window support for controlling the scope of aggregations. Implement a ROLLING_WINDOW.ROLLING_WINDOW: A sliding time window where you want to set a range and always keep the range intact while moving forward in time. This is a time window that never collapses.

For example, rolling_window(count(target_event), 5 mins, 1 min) -> counts the number of occurrences of a target event in the last 5 mins. After 1 mins, the count will be set to sum(count(2nd min), count(3rd min), count(4th min), count(5th min), count(6th min) ~ 0)

Following is the expected schema for different type on events:
eventType (String)
timestamp (Date time format)
eventData {
    key1: value1
    key2: value2
    key3: value3
    ………
}

The AggregationMeta table holds the metadata for the aggregations configured. Store this info in-memory for simplicity of the implementation. No need to connect to any database.
To create a new aggregation, a new record needs to be created in the AggregationMeta table. Following is the schema for aggregation meta:

aggregationName (String | unique)
eventType (Event type of the event on which aggregation needs to be performed)
aggregationType (enum: ["count", "sum"])
aggregationKey (String)
aggregationField (Json path of field to be aggregated)
timeInterval (integer | in mins)
slideInterval (integer | in mins)
filterRules (Rules to filter events to be considered for aggregation) (enum: ["eq", "gt", "lt"])

Consider the following example for more clarity:
Following are the "APP_INSTALL_COMPLETED" events for different users of app store:

{
    "eventType": "APP_INSTALL_COMPLETED",
    "timestamp": "1706452811465",
    "eventData": {
        "appName": "app1",
        "source": "home",
        "userId": "u001" ,
        "spaceInMb": 2
    }
}

{
    "eventType": "APP_INSTALL_COMPLETED",
    "timestamp": "1706452811500",
    "eventData": {
        "appName": "app2",
        "source": "home",
        "userId": "u001",
        "spaceInMb": 3
    }
}

{
    "eventType": "APP_INSTALL_COMPLETED",
    "timestamp": "1706452811520",
    "eventData": {
        "appName": "app2",
        "source": "home",
        "userId": "u002",
        "spaceInMb": 3
    }
}

{
    "eventType": "APP_INSTALL_COMPLETED",
    "timestamp": "1706452811675",
    "eventData": {
        "appName": "app3",
        "source": "home",
        "userId": "u002",
        "spaceInMb": 4
    }
}

SCENARIO 1: We need a total number of installs done by each user where spaceInMb size is greater than 2 in the last 5 mins. Following will be the aggregation record for the above example.

{
    aggregationName: "installCountUser",
    eventType: "APP_INSTALL_COMPLETED",
    aggregationType: "count",
    aggregationKey: "eventData.userId",
    aggregationField: "eventData.spaceInMb",
    timeInterval: 5,
    slideInterval: 1,
    filterRules: [
        {
            "field": "eventData.spaceInMb",
            "op": "gt",
            "value": 2
        }
    ]
}
Following are the aggregations created for the above metadata:

"u001": 1
"u002": 2

SCENARIO 2: If we need the total space utilized by each user to install all the apps. Following will be the aggregation metadata for this.

{
    aggregationName: "totalSpaceUser",
    eventType: "APP_INSTALL_COMPLETED",
    aggregationType: "sum",
    aggregationKey: "eventData.userId",
    aggregationField: "eventData.spaceInMb",
    timeInterval: 5,
    slideInterval: 1,
    filterRules: []
}
Following are the aggregates created for the above metadata:

"u001": 5
"u002": 7

Store these aggregates also in memory for simplicity purposes.

These aggregates should not be computed in real time. All of them should be pre computed and just retrieved from memory when requested.

Expected Interfaces:

createMeta(AggregationMeta aggregationMeta)
// Pass aggregation meta information to this function to create a new aggregation meta record

Following is the sample input for this function

{
    aggregationName: "installCountUser",
    eventType: "APP_INSTALL_COMPLETED",
    aggregationType: "count",
    aggregationKey: "eventData.userId",
    aggregationField: "eventData.spaceInMb",
    filterRules: [
        {
            "field": "eventData.spaceInMb",
            "op": "gt",
            "value": 2
        }
    ]
}
getAggregation(aggregationName, keyValue)

// getAggregation("installCountUser", "u002") will give output as 2.

Important note: This function should not calculate aggregation in real time. It should only get the pre-computed value stored in memory.

Expectations
Code should be working properly
Separation of concerns
Abstractions
Application of OO design principles
Testability
Code readability

[execution time limit] 4 seconds (js)

[memory limit] 1 GB

Comments