הכנה לראיון עבודהJob interview prep
שאלות נפוצות, קוד על הלוח, פרויקטים בקורות חיים.
Common questions, whiteboard coding, projects on your CV.
שני הגדוליםSecond largest
תזכורת: מה ביקשוReminder: the task
כתבו second_largest(nums) שמחזירה את הערך השני בגודלו מבין הערכים השונים, או None אם אין כזה. בלי מיון: מעבר אחד עם שני משתנים.
Write second_largest(nums) returning the second largest distinct value, or None if there isn't one. No sorting: one pass with two variables.
הפתרון המלא והפלט שלוFull solution and its output
def second_largest(nums):
first = second = None
for x in nums:
if first is None or x > first:
first, second = x, first
elif x != first and (second is None or x > second):
second = x
return second
print(second_largest([412, 96, 604, 604, 240]), second_largest([5, 5]), second_largest([]))412 None None
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
אנגרמות בקבוצותGrouping anagrams
תזכורת: מה ביקשוReminder: the task
כתבו group_anagrams(words) שמחזירה רשימת קבוצות של מילים שהן אנגרמות זו של זו (בלי רגישות לאותיות). כל קבוצה בסדר ההופעה, והקבוצות לפי הופעת המילה הראשונה שלהן.
Write group_anagrams(words) returning groups of words that are anagrams of each other (case-insensitive). Each group in order of appearance, and groups ordered by their first word's appearance.
הפתרון המלא והפלט שלוFull solution and its output
def group_anagrams(words):
groups = {}
for w in words:
groups.setdefault("".join(sorted(w.lower())), []).append(w)
return list(groups.values())
print(group_anagrams(["listen", "Dune", "silent", "Mame", "enlist", "Emma"]))[['listen', 'silent', 'enlist'], ['Dune'], ['Mame', 'Emma']]
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
סוגריים מאוזנים בשני מצביעים? לא, במחסניתBalanced brackets: a stack, not pointers
תזכורת: מה ביקשוReminder: the task
כתבו valid(s) שבודקת אם כל סוגי הסוגריים ()[]{} במחרוזת מאוזנים ובסדר נכון (תווים אחרים מתעלמים). השתמשו ברשימה כמחסנית.
Write valid(s) checking whether all bracket kinds ()[]{} in a string are balanced and correctly nested (other characters ignored). Use a list as a stack.
הפתרון המלא והפלט שלוFull solution and its output
def valid(s):
pairs = {")": "(", "]": "[", "}": "{"}
stack = []
for c in s:
if c in "([{":
stack.append(c)
elif c in pairs:
if not stack or stack.pop() != pairs[c]:
return False
return not stack
print(valid("print(shelf[0]{1})"), valid("(]"), valid("(("), valid(""))True False False True
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
רצף קריאה הכי טובThe best reading run
תזכורת: מה ביקשוReminder: the task
כתבו best_run(pages, k) שמחזירה (סכום, אינדקס התחלה) של k הימים הרצופים עם הכי הרבה עמודים, ב-O(n) עם חלון נע. אם k גדול מהרשימה או 0, מחזירים (0, None). בשוויון: החלון המוקדם.
Write best_run(pages, k) returning (sum, start index) of the k consecutive days with the most pages, in O(n) with a sliding window. If k exceeds the list or is 0, return (0, None). On a tie: the earlier window.
הפתרון המלא והפלט שלוFull solution and its output
def best_run(pages, k):
if k <= 0 or k > len(pages):
return 0, None
window = sum(pages[:k])
best, start = window, 0
for i in range(k, len(pages)):
window += pages[i] - pages[i - k]
if window > best:
best, start = window, i - k + 1
return best, start
print(best_run([20, 35, 0, 40, 60, 10, 0, 80], 3), best_run([1, 2], 5))(110, 3) (0, None)
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
מדף, צעד 35: הצגה ב-3 דקותShelf, step 35: a 3-minute pitch
תזכורת: מה ביקשוReminder: the task
כתבו check_pitch(parts) שמקבלת רשימת (name, seconds, line) ומחזירה רשימת בעיות (ריקה = מוכן): "too long" אם הסכום מעל 180, "too short" מתחת ל-120, "start with the problem" אם החלק הראשון אינו Problem, "no demo" אם אין חלק Demo, ו-"long line: <name>" לכל שורה מעל 25 מילים. אחר כך כתבו את ההצגה שלכם על המדף כך שתעבור.
Write check_pitch(parts) taking (name, seconds, line) tuples and returning a list of problems (empty = ready): "too long" if the total exceeds 180, "too short" under 120, "start with the problem" if the first part isn't Problem, "no demo" if there's no Demo part, and "long line: <name>" for any line over 25 words. Then write your own shelf pitch so it passes.
הפתרון המלא והפלט שלוFull solution and its output
def check_pitch(parts):
problems = []
total = sum(s for _, s, _ in parts)
if total > 180:
problems.append("too long")
if total < 120:
problems.append("too short")
if not parts or parts[0][0] != "Problem":
problems.append("start with the problem")
if "Demo" not in [n for n, _, _ in parts]:
problems.append("no demo")
problems += [f"long line: {n}" for n, _, line in parts if len(line.split()) > 25]
return problems
my_pitch = [
("Problem", 20, "I lost track of what I read and how far I got."),
("What I built", 40, "Shelf: a Python CLI with a Book dataclass, safe JSON storage, SQLite and a FastAPI API."),
("Demo", 60, "Add a book, log pages, see stats and the monthly chart."),
("Challenge", 40, "Atomic saves through a temp file, covered by pytest."),
]
print(check_pitch(my_pitch) or "ready")
print(check_pitch([("Setup", 300, "word " * 30)]))ready ['too long', 'start with the problem', 'no demo', 'long line: Setup']
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
המחרוזת הארוכה בלי חזרהLongest substring without repeats
תזכורת: מה ביקשוReminder: the task
כתבו longest_unique(s) שמחזירה את תת-המחרוזת הרציפה הארוכה ביותר בלי תו חוזר (בשוויון: הראשונה), ב-O(n) עם חלון נע ומילון של המיקום האחרון של כל תו.
Write longest_unique(s) returning the longest contiguous substring with no repeated character (on a tie: the first), in O(n) with a sliding window and a dict of each character's last position.
הפתרון המלא והפלט שלוFull solution and its output
def longest_unique(s):
last, start, best = {}, 0, (0, 0)
for i, c in enumerate(s):
if c in last and last[c] >= start:
start = last[c] + 1
last[c] = i
if i - start + 1 > best[1] - best[0]:
best = (start, i + 1)
return s[best[0]:best[1]]
for t in ["bookshelf", "abcabcbb", "pwwkew", ""]:
print(repr(t), "->", repr(longest_unique(t)))'bookshelf' -> 'okshelf' 'abcabcbb' -> 'abc' 'pwwkew' -> 'wke' '' -> ''
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.
ספרים לפי תקציבBooks on a budget
תזכורת: מה ביקשוReminder: the task
יש לכם תקציב ורשימת ספרים עם מחיר. כתבו max_books(prices, budget) שמחזירה את מספר הספרים המקסימלי שאפשר לקנות (חמדני: הזולים קודם), ו-exact_pair(prices, budget) שמחזירה זוג אינדקסים (ממוין) של שני ספרים שמחירם יחד בדיוק budget, או None, ב-O(n) עם מילון.
You have a budget and a list of book prices. Write max_books(prices, budget) returning the most books you can buy (greedy: cheapest first), and exact_pair(prices, budget) returning a (sorted) pair of indexes of two books costing exactly budget together, or None, in O(n) with a dict.
הפתרון המלא והפלט שלוFull solution and its output
def max_books(prices, budget):
count = 0
for p in sorted(prices):
if p > budget:
break
budget -= p
count += 1
return count
def exact_pair(prices, budget):
seen = {}
for i, p in enumerate(prices):
if budget - p in seen:
return (seen[budget - p], i)
seen.setdefault(p, i)
return None
prices = [59.9, 42.0, 38.5, 36.0, 49.9]
print(max_books(prices, 120), exact_pair([40, 25, 60, 35], 75), exact_pair([10, 20], 100))3 (0, 3) None
יש יותר מדרך נכונה אחת. אם הפלט שלכם זהה, גם הפתרון שלכם נכון.There is more than one right way. If your output matches, your solution is right too.