Nested Event Group Counts
Updated
Problem statement
You are given a list of events. Each event has some properties, and each property has a name and a string value. Event i is stored in two arrays of the same length: fieldNames[i] holds its property names, and fieldValues[i] holds the matching values in the same positions.
You are also given groupProperties, an ordered list of different property names. Every event has each of these properties.
Group the events by the first property in groupProperties. Inside each group, split the events again by the next property, and so on until the end of groupProperties. At the last level, store how many events have that exact combination of values.
Return the nested groups as one JSON string, written in this exact form:
- at every level, object keys are sorted lexicographically, comparing characters by their ASCII codes,
- there is no whitespace outside quoted strings, and
- each count at the last level is written as a plain decimal number.
Function
countEventGroups(fieldNames: String[][], fieldValues: String[][], groupProperties: String[]) → StringExamples
Example 1
fieldNames = [["type","country","browser"],["country","browser","type"],["browser","type","country"],["type","country","browser"]]fieldValues = [["click","US","Chrome"],["CA","Safari","click"],["Firefox","view","US"],["click","US","Edge"]]groupProperties = ["type","country"]return = "{\"click\":{\"CA\":1,\"US\":2},\"view\":{\"US\":1}}"Events are grouped by type first, then by country inside each type. The click events come from CA once and from US twice. The only view event comes from US. Keys are sorted, so CA comes before US, which gives the output shown.
Example 2
fieldNames = [["type","country"],["country","type"],["type","country"]]fieldValues = [["signup","US"],["DE","purchase"],["signup","CA"]]groupProperties = ["country"]return = "{\"CA\":1,\"DE\":1,\"US\":1}"Only country is in groupProperties, so the result is a single flat object. Each country appears in exactly one event, and the keys are sorted: CA, DE, US.
Example 3
fieldNames = [["type","country","browser"],["type","country","browser"],["type","country","browser"]]fieldValues = [["click","US","Chrome"],["click","US","Safari"],["click","US","Chrome"]]groupProperties = ["type","country","browser"]return = "{\"click\":{\"US\":{\"Chrome\":2,\"Safari\":1}}}"There are three properties in groupProperties, so the result has three nested levels: type, then country, then browser. Every event is a click from US. The first and last events both use Chrome, so Chrome gets count 2. Only the middle event uses Safari.
Constraints
1 <= fieldNames.length == fieldValues.length <= 1000001 <= fieldNames[i].length == fieldValues[i].length <= 20- Property names within each event are distinct.
1 <= groupProperties.length <= 5, and its property names are distinct.- Every event contains every property in
groupProperties. - Property names and values are nonempty printable ASCII strings of length at most 50.
More Mixpanel Phone Screen questions
Similar questions at other companies
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Nested Set Structural EquivalencePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Minimum Dictionary SegmentsGoogle · PHONE SCREENGoogle interview questions
- Longest Substring with At Least K Repeating CharactersBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Validate Nested JSON Against a SchemaBenchling · ONSITE INTERVIEWBenchling interview questions
- Recursive Character FrequencyPalo Alto Networks · ONSITE INTERVIEWPalo Alto Networks interview questions
- Validate a Nested Object Against a SchemaConfluent · ONSITE INTERVIEWConfluent interview questions
- Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions