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
Custom Utility Without Prototypes
00:00
5 left

Custom Utility Without Prototypes

MediumPython

Problem

QuickBooks frontend utilities may receive nested category values that need to be flattened before rendering or validation. Implement a custom utility that behaves like JavaScript's Array.prototype.flat, without calling built-in prototype methods such as flat, map, reduce, concat, or join.

Given a list containing values or nested lists and a nonnegative integer depth, return a new list in left-to-right order. Flatten a nested list only when the current remaining depth is greater than zero. Non-list values must be copied into the result unchanged. A depth of 0 returns a shallow copy, preserving nested lists as elements.

The implementation should use explicit iteration or recursion and must not mutate the input list.

Constraints

  • 0 <= len(values) <= 10^4
  • Nesting depth is at most 1000
  • 0 <= depth <= 1000
  • Each element is either a list or a non-list value
  • Preserve the relative order of all output elements
  • Do not mutate values

Function Signature

def flatten_values(values, depth):
Interviewer

Your question is Custom Utility Without Prototypes. 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.