Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Custom Utility Without Prototypes

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Custom Utility Without Prototypes. Start with the requirements on the right.

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.

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 Python 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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output