FastPrepNested Event Group Counts
Problems/Nested Event Group Counts

Nested Event Group Counts

Mixpanel logoMixpanel● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Mixpanel phone screen for a full-time role. The original report describes recursive grouping by an ordered list of properties, with counts at the leaves. It does not say what happens when a property is missing. It also gives no portable event layout, no output format, and no limits. Here the events come as parallel field arrays. Every requested property must be present. The answer is sorted JSON in one fixed form.

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[]) → String

Examples

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 <= 100000
  • 1 <= 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

  • Format a Centered Pagination BarPHONE SCREEN · Seen Jul 2020

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
All Mixpanel interview questionsSee Mixpanel hiring insights
Saved
class Solution {
    public String countEventGroups(String[][] fieldNames, String[][] fieldValues, String[] groupProperties) {
        // Write your code here.
    }
}
Checking account…