Number of Transactions per Visit
Imagine you're analyzing website traffic data to understand user behavior. You want to determine how many transactions (e.g., purchases, sign-ups) occur during each visit to your site. This information can help you identify areas for improvement in your user experience and optimize conversion rates.
Problem Description
You are given a log of user interactions on a website. Each interaction is represented as a record containing a visit_id (unique identifier for a user's session) and an event_type (e.g., "page_view", "add_to_cart", "purchase"). Your task is to calculate the number of transactions (defined as events with event_type equal to "purchase") that occur during each unique visit_id.
What needs to be achieved:
- Process a list of interaction records.
- Group interactions by
visit_id. - Count the number of "purchase" events within each
visit_idgroup. - Return a mapping (e.g., dictionary, hashmap) where the keys are
visit_ids and the values are the corresponding number of purchases.
Key Requirements:
- The input is a list of records, where each record is a dictionary/map with keys
visit_idandevent_type. - The
visit_idis a string. - The
event_typeis a string. - The output should be a mapping (dictionary/hashmap) where keys are
visit_ids and values are the number of purchases for that visit. - If a
visit_idhas no purchases, its corresponding value in the output should be 0.
Expected Behavior:
The solution should correctly count purchases for each visit, even if visits have multiple page views or other non-purchase events. It should handle cases where a visit has no purchases.
Edge Cases to Consider:
- Empty input list.
visit_idvalues that are empty strings.event_typevalues that are not "purchase" (should be ignored).- Duplicate records with the same
visit_idandevent_type. (Count each purchase only once).
Examples
Example 1:
Input: [
{"visit_id": "A123", "event_type": "page_view"},
{"visit_id": "A123", "event_type": "add_to_cart"},
{"visit_id": "A123", "event_type": "purchase"},
{"visit_id": "B456", "event_type": "page_view"},
{"visit_id": "B456", "event_type": "purchase"},
{"visit_id": "C789", "event_type": "page_view"},
{"visit_id": "C789", "event_type": "page_view"},
{"visit_id": "C789", "event_type": "purchase"},
{"visit_id": "C789", "event_type": "purchase"}
]
Output: {"A123": 1, "B456": 1, "C789": 2}
Explanation: Visit A123 has 1 purchase, B456 has 1 purchase, and C789 has 2 purchases.
Example 2:
Input: [
{"visit_id": "X999", "event_type": "page_view"},
{"visit_id": "Y000", "event_type": "page_view"},
{"visit_id": "Z111", "event_type": "page_view"}
]
Output: {"X999": 0, "Y000": 0, "Z111": 0}
Explanation: None of the visits have any purchases, so the count for each is 0.
Example 3: (Edge Case)
Input: []
Output: {}
Explanation: The input list is empty, so the output is an empty mapping.
Constraints
- The input list can contain up to 10,000 records.
visit_idandevent_typeare strings with a maximum length of 255 characters.- The solution should have a time complexity of O(n), where n is the number of records in the input list.
- The solution should use a reasonable amount of memory (e.g., avoid storing the entire input list in memory if possible).
Notes
Consider using a hash map (dictionary) to efficiently group interactions by visit_id. Iterate through the input list once, updating the purchase count for each visit_id as you encounter it. Remember to handle the case where a visit_id is encountered for the first time. Focus on clarity and efficiency in your solution.