שחזור של ראיון עבודה טלפוני JP MORGAN - ג'י פי מורגן
Had a phone interview with an online python shell allowing the interviewer to see what i write & its output.
The question was:
Given a set, implement a function that returns all subsets of that set.
I did it in python (I'm guessing that had i chosen a different programming language - I would have gotten a different question )
Here's my solution:
>>> def subsets(s):
"""
Get all subsets of s
:param s: list of unique elements (mathematical set)
:return: list of (lists) s's subsets
"""
_subsets = []
for i in range(len(s)+1):
_subsets.extend(combinations(s,i))
return _subsets
>>> def combinations(s, n):
"""
Get all n length combinations from s
:param s: list of unique elements (mathematical set)
:param n: number of elements in each combination
:return: list of (lists) all n length combinations that can b combined from s
"""
if n == 0:
return [[]]
s_len = len(s)
if s_len == n:
return [s]
subsets_s = []
for i in range(s_len):
for subset in combinations(s[i+1:], n-1):
subsets_s.append([s[i]] + subset)
return subsets_s
>>> subsets(range(5))
[[], [0], [1], [2], [3], [4], [0, 1], [0, 2], [0, 3], [0, 4], [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [0, 1, 2, 3], [0, 1, 2, 4], [0, 1, 3, 4], [0, 2, 3, 4], [1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>>
* השחזור נשלח ע"י חבר קהילת JobHunt ופורסם באישור ובדיסקרטיות
עם המודל של ג'ובהאנט מגיעים לפי 2 ראיונות עבודה ב-1/2 מהזמן!