Nested Event Group Counts
Learn this problemProblem statement
You are given a collection of events. Event i is represented by two parallel arrays: fieldNames[i] contains its property names, and fieldValues[i] contains the corresponding string values.
You are also given an ordered array groupProperties of distinct property names. Every event contains each requested property.
Group the events recursively in the order specified by groupProperties. At the final depth, store the number of events with that complete sequence of property values.
Return the nested result as canonical JSON:
- object keys are sorted lexicographically at every level,
- there is no whitespace outside quoted strings, and
- leaf values are decimal event counts.
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}}"The first grouping level is type. Within each type, events are grouped by country, and the leaves contain event counts.
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 requested, so the result has one object level.
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}}}"The three requested properties create three object levels. The repeated Chrome path has count 2.
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.