Unit 2 Session 1 (Click for link to problem statements)
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?
a
is empty?True
because an empty list is a subset of any list. b
have duplicates?b
do not affect the result.HAPPY CASE
Input: a = [1, 2], b = [1, 2, 3]
Output: True
Explanation: Every element in a exists in b.
Input: a = [3, 3], b = [1, 2, 3]
Output: True
Explanation: All instances of 3 in a exist in b.
EDGE CASE
Input: a = [], b = [1, 2, 3]
Output: True
Explanation: An empty list is trivially a subset.
Input: a = [4], b = [1, 2, 3]
Output: False
Explanation: 4 does not exist in b.
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Check if every element in list a
is present in list b
.
1) Iterate through each element in list a
:
a) If the element is not in b
, return False
2) After looping through the full list, return True
Implement the code to solve the algorithm.
def all_in(a, b):
for elem in a:
if elem not in b:
return False
return True