Unit 3 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Loop from 0 to the longer length, and if valid, add an element from each list at that index,
1) Create an empty list for results
2) Find the length of the longer list
3) For each index from 0 to the longer length:
a) If index is valid in lst1, add that element to results
b) If index is valid in lst2, add that element to results
4) Return the results list
⚠️ Common Mistakes
def interleave_lists(lst1, lst2):
result = []
lst1_len = len(lst1)
lst2_len = len(lst2)
length_of_longest_list = max(lst1_len, lst2_len)
for i in range(length_of_longest_list):
if i < lst1_len:
result.append(lst1[i])
if i < lst2_len:
result.append(lst2[i])
return result