Codepath

Find the Difference of Two Signal Arrays

Unit 2 Session 1 (Click for link to problem statements)

U-nderstand

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

  • Q: What is the problem asking for?

    • A: The problem asks to return a list containing two lists: one with distinct integers from signals1 not in signals2, and one with distinct integers from signals2 not in signals1.
  • Q: What are the inputs?

    • A: Two 0-indexed integer arrays signals1 and signals2.
  • Q: What are the outputs?

    • A: A list of size 2 where the first element is a list of integers from signals1 not in signals2, and the second element is a list of integers from signals2 not in signals1.
  • Q: Are there any constraints on the values in the arrays?

    • A: The integers may appear in any order in the result lists.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Use sets to find the differences between the two lists and convert the results back to lists.

1. Convert `signals1` and `signals2` to sets `set1` and `set2`.
2. Find the difference between `set1` and `set2` and store it in `diff1`.
3. Find the difference between `set2` and `set1` and store it in `diff2`.
4. Convert `diff1` and `diff2` to lists.
5. Return the list `[diff1, diff2]`.

I-mplement

def find_difference(signals1, signals2):
    # Step 1: Convert the lists to sets
    set1 = set(signals1)
    set2 = set(signals2)
    
    # Step 2: Find the difference between the sets
    diff1 = list(set1 - set2)
    diff2 = list(set2 - set1)
    
    # Step 3: Return the differences as lists
    return [diff1, diff2]
Fork me on GitHub