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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
IsFollowing Implementation
00:00
5 left

IsFollowing Implementation

EasyPython

Problem

Uber social features may need to answer whether a user followed another user at a particular historical snapshot. Each snapshot is represented as a node in a persistent history: it stores one follow or unfollow change and a pointer to the previous snapshot.

Implement is_following(followerId, followeeId, snapId) to return whether followerId follows followeeId in the state represented by snapId. The newest change for a relationship determines its state. If no change for the pair exists in the snapshot's history, return False.

Formal Specification

  • followerId and followeeId are integers.
  • snapId is either None, representing an empty snapshot, or a dictionary with:
    • id: an integer snapshot identifier
    • change: a dictionary containing followerId, followeeId, and action, where action is either "follow" or "unfollow"
    • parent: the previous snapshot node or None
  • Return a boolean.
  • Snapshot nodes form a finite singly linked chain ending at None.

Walk from the requested snapshot toward its ancestors. The first matching relationship change is authoritative.

Constraints

  • 0 <= followerId, followeeId <= 10^9
  • The snapshot history contains at most 10^5 nodes
  • Every parent pointer eventually reaches None
  • A user cannot follow themselves

Function Signature

def is_following(followerId, followeeId, snapId):
Interviewer

Your question is IsFollowing Implementation. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.