Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Build User Action Tree
00:00
5 left

Build User Action Tree

HardPython

Problem

Design and implement code to build a user action tree from application logs.

Implement build_action_trees(logs). Each log is a dictionary with id, user_id, action, parent_id, and timestamp. parent_id is None for a root action. Return a dictionary mapping each user ID to a list of root nodes, where each node has id, action, and children. Logs may be unordered; roots and children must be ordered by (timestamp, id). Every non-root parent exists for the same user, and action IDs are globally unique.

Input: logs, a list of dictionaries. Output: a dictionary of nested action trees.

Constraints

  • 0 <= len(logs) <= 10^5
  • Each log contains id, user_id, action, parent_id, and timestamp
  • Action IDs are globally unique
  • parent_id is null for roots
  • Every non-null parent_id refers to an action for the same user
  • The action relationships form forests with no cycles
  • Timestamps and IDs define a deterministic sibling order

Function Signature

def build_action_trees(logs):
Interviewer

Your question is Build User Action Tree. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.