Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten a Nested List

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

Your question is Flatten a Nested List. 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

How would you implement a function to flatten a nested list?

Implement def flatten_nested_list(nested):, where nested contains integers and lists nested to arbitrary depth. Return a single list containing all integers in their original left-to-right order, including repeated values and ignoring empty lists.

Constraints

  • The input is a list containing integers or other lists.
  • Nesting depth may be arbitrary.
  • The total number of integers and list elements is at most 1000.
  • Integer values are between -10^9 and 10^9.
  • The relative order of all integer values must be preserved.

Function Signature

def flatten_nested_list(nested):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output