Skip to content

Flatten List

Andrew Burke edited this page Jan 10, 2025 · 2 revisions

JCSU Unit 2 Problem Set 1 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Nested Loops, Iteration, List Flattening

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is the goal of the problem?
    • The goal is to flatten a list of lists into a single list containing all elements in the same order.
  • Are there constraints on input?
    • The input will always be a list of lists.

HAPPY CASE Input: nested_list = [ [1, 2], [3, 4], [5, 6] ] Output: [1, 2, 3, 4, 5, 6] Explanation: The nested list is flattened to a single list containing all elements.

EDGE CASE Input: nested_list = [[]] Output: [] Explanation: An empty list of lists results in an empty flattened list.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For list flattening problems, we want to consider the following approaches:

  • Nested Loops: Use a nested loop to iterate through each sublist and extract elements.
  • List Comprehension: Use Python list comprehensions for concise syntax (alternative solution).

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea:
Iterate through each sublist in the nested list, and then iterate through each element in the sublist. Append all elements to a new list in order.

Steps:

  1. Initialize an empty list flattened to store the flattened elements.
  2. For each sublist in the input nested_list:
    • For each element in the sublist:
      • Append the element to flattened.
  3. Return the flattened list.

4: I-mplement

Implement the code to solve the algorithm.

def flatten_list(nested_list):
    flattened = []  # Initialize an empty list to store the flattened elements
    for sublist in nested_list:  # Iterate through each sublist in the nested list
        for element in sublist:  # Iterate through each element in the sublist
            flattened.append(element)  # Append the element to the flattened list
    return flattened  # Return the flattened list

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

Example 1:

  • Input: nested_list = [ [1, 2], [3, 4], [5, 6] ]
  • Expected Output: [1, 2, 3, 4, 5, 6]
  • Observed Output: [1, 2, 3, 4, 5, 6]

Example 2:

  • Input: nested_list = [[]]
  • Expected Output: []
  • Observed Output: []

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume n is the total number of elements in all sublists.

  • Time Complexity: O(n) because we iterate through each element exactly once.
  • Space Complexity: O(n) because we create a new flattened list to store all elements.
Clone this wiki locally