נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

אלגוריתמים, ראיון עבודה ופרויקט גמרAlgorithms, interviews & final project

מפגש 42: סיבוכיות, חיפוש ומיון, שאלות ראיון נפוצות ב-Java, ותכנון פרויקט הגמר.Session 42: Complexity, searching and sorting, common Java interview questions, and planning the final project.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • חיפוש בינאריBinary search
  • Two Sum עם HashMapTwo Sum with a HashMap
  • מיון הכנסהInsertion sort
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • חיפוש בינארי: חיפוש במערך ממוין:binary search: Searching a sorted array:
  • שני מצביעים: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני).two pointers: Two indexes moving over the array (from the ends inward, or one behind the other).
  • BFS: חיפוש לרוחב:BFS: Breadth-first search:
  • שיטה לשאלת ראיון: מבינים את השאלה ושואלים על מקרי קצה, פותרים קודם בדרך הפשוטה, אומרים את הסיבוכיות, משפרים (לרוב עם HashMap או מיון), ובודקים על דוגמה.an interview method: Understand the question and ask about edge cases, solve it the simple way first, state the complexity, improve (often with a HashMap or sorting), and test on an example.
נושאTopic 2

חיפוש בינאריBinary search

חיפוש בינאריBinary search

  • במערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני.In a sorted array, check the middle: if it's smaller than the target throw away the left half, if bigger the right.
  • בסימולציה רואים את lo, hi ו-mid מתכנסים.In the simulation you see lo, hi and mid close in.
נושאTopic 3

Two Sum עם HashMapTwo Sum with a HashMap

Two Sum עם HashMapTwo Sum with a HashMap

  • שאלת הראיון הנפוצה ביותר: אילו שני מספרים מסתכמים ל-16?The most common interview question: which two numbers add up to 16?
  • במקום לבדוק כל זוג (O(n²)), זוכרים מה כבר ראינו ובודקים אם "המשלים" קיים: O(n).Instead of checking every pair (O(n²)), remember what you've seen and check whether the "complement" exists: O(n).
נושאTopic 4

מיון הכנסהInsertion sort

מיון הכנסהInsertion sort

  • כל איבר "נכנס" למקום שלו בחלק הממוין שמשמאלו, כמו קלפים ביד.Each item "slides" into place in the sorted part to its left, like cards in a hand.
  • O(n²) במקרה הרע, אבל מהיר על מערך כמעט ממוין.O(n²) at worst, but fast on an almost-sorted array.
  • ב-Java אמיתית: Arrays.sort.In real Java: Arrays.sort.
נושאTopic 5

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves

חיפוש בינארי: חיפוש במערך ממוין:binary search: Searching a sorted array:

שני מצביעים: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני).two pointers: Two indexes moving over the array (from the ends inward, or one behind the other).

BFS: חיפוש לרוחב:BFS: Breadth-first search:

שיטה לשאלת ראיון: מבינים את השאלה ושואלים על מקרי קצה, פותרים קודם בדרך הפשוטה, אומרים את הסיבוכיות, משפרים (לרוב עם HashMap או מיון), ובודקים על דוגמה.an interview method: Understand the question and ask about edge cases, solve it the simple way first, state the complexity, improve (often with a HashMap or sorting), and test on an example.

במערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני.In a sorted array, check the middle: if it's smaller than the target throw away the left half, if bigger the right.

בסימולציה רואים את lo, hi ו-mid מתכנסים.In the simulation you see lo, hi and mid close in.

שאלת הראיון הנפוצה ביותר: אילו שני מספרים מסתכמים ל-16?The most common interview question: which two numbers add up to 16?

במקום לבדוק כל זוג (O(n²)), זוכרים מה כבר ראינו ובודקים אם "המשלים" קיים: O(n).Instead of checking every pair (O(n²)), remember what you've seen and check whether the "complement" exists: O(n).

כל איבר "נכנס" למקום שלו בחלק הממוין שמשמאלו, כמו קלפים ביד.Each item "slides" into place in the sorted part to its left, like cards in a hand.

O(n²) במקרה הרע, אבל מהיר על מערך כמעט ממוין.O(n²) at worst, but fast on an almost-sorted array.

ב-Java אמיתית: Arrays.sort.In real Java: Arrays.sort.

מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves

כמה צעדים לחיפוש בינארי במערך של מיליון?How many steps does a binary search take on a million items?

  • כ-20About 20
  • מיליוןA million
  • 10001000
  • 22

כ-20About 20

מה התנאי לחיפוש בינארי?What does a binary search require?

  • מערך ממויןA sorted array
  • מערך קטןA small array
  • HashMapA HashMap
  • מספרים חיובייםPositive numbers

מערך ממויןA sorted array

בראיון קיבלתם שאלה. מה עושים קודם?You got an interview question. What first?

  • מוודאים שהבנתם ושואלים על מקרי קצהMake sure you understood and ask about edge cases
  • כותבים את הפתרון הכי מהיר מידWrite the fastest solution right away
  • שותקים וחושבים 10 דקותThink silently for 10 minutes
  • שואלים מה התשובהAsk for the answer

מוודאים שהבנתם ושואלים על מקרי קצהMake sure you understood and ask about edge cases

חיפוש בינאריBinary search

  • כתבו find(sorted, target) שמחזירה אינדקס או -1.Write find(sorted, target) returning an index or -1.

פלינדרום עם שני מצביעיםA palindrome with two pointers

  • isPalindrome(s) מתעלמת מאותיות גדולות, רווחים וסימנים, ומשווה מהקצוות פנימה.isPalindrome(s) ignores capitals, spaces and punctuation, and compares from the ends inward.

התו הראשון שלא חוזרThe first non-repeating character

  • firstUnique(s) מחזירה את התו הראשון שמופיע פעם אחת (או -).firstUnique(s) returns the first character appearing once (or -).

מיון הכנסהInsertion sort

  • כתבו את מיון ההכנסה מהתזכורת ומיינו {9, 3, 7, 3, 1}:Write the insertion sort from the refresher and sort {9, 3, 7, 3, 1}:

מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves

  • שני מדפים ממוינים לפי שנה.Two shelves sorted by year.

פיבונאצ'י בלולאהFibonacci in a loop

  • fib(n) בלולאה עם שני משתנים, O(n).fib(n) in a loop with two variables, O(n).

BFS: כמה צעדים בין ספריםBFS: how many steps between books

  • מפה של "ספרים דומים".A map of "similar books".

while (lo <= hi): עם < מפספסים את האיבר האחרון.while (lo <= hi): with < you miss the last item.

replaceAll("[^a-z]", "") אחרי toLowerCase.replaceAll("[^a-z]", "") after toLowerCase.

O(n): שני מעברים עדיין לינאריים.O(n): two passes are still linear.

מה למדנוWhat we learned

  • חיפוש בינארי: חיפוש במערך ממוין:binary search: Searching a sorted array:
  • במערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני.In a sorted array, check the middle: if it's smaller than the target throw away the left half, if bigger the right.
  • שאלת הראיון הנפוצה ביותר: אילו שני מספרים מסתכמים ל-16?The most common interview question: which two numbers add up to 16?
  • כל איבר "נכנס" למקום שלו בחלק הממוין שמשמאלו, כמו קלפים ביד.Each item "slides" into place in the sorted part to its left, like cards in a hand.
  • מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich