נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
אלגוריתמים, ראיון עבודה ופרויקט גמרAlgorithms, interviews & final project
מפגש 42: סיבוכיות, חיפוש ומיון, שאלות ראיון נפוצות ב-Java, ותכנון פרויקט הגמר.Session 42: Complexity, searching and sorting, common Java interview questions, and planning the final project.
סיבוכיות, חיפוש ומיון, שאלות ראיון נפוצות ב-Java, ותכנון פרויקט הגמר. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Complexity, searching and sorting, common Java interview questions, and planning the final project. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- חיפוש בינאריBinary search
- Two Sum עם HashMapTwo Sum with a HashMap
- מיון הכנסהInsertion sort
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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.
במערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני. בסימולציה רואים את lo, hi ו-mid מתכנסים.In a sorted array, check the middle: if it's smaller than the target throw away the left half, if bigger the right. 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).
שאלת הראיון הנפוצה ביותר: אילו שני מספרים מסתכמים ל-16? במקום לבדוק כל זוג (O(n²)), זוכרים מה כבר ראינו ובודקים אם "המשלים" קיים: O(n).The most common interview question: which two numbers add up to 16? 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.
כל איבר "נכנס" למקום שלו בחלק הממוין שמשמאלו, כמו קלפים ביד. O(n²) במקרה הרע, אבל מהיר על מערך כמעט ממוין. ב-Java אמיתית: Arrays.sort.Each item "slides" into place in the sorted part to its left, like cards in a hand. O(n²) at worst, but fast on an almost-sorted array. In real Java: Arrays.sort.נושאTopic 5
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves
שני מדפים ממוינים לפי שנה. mergeByYear(a, b) ממזגת לרשימה ממוינת אחת ב-O(n) (בלי למיין מחדש). הדפיסו Emma Ulysses Solaris Dune Beloved.Two shelves sorted by year. mergeByYear(a, b) merges them into one sorted list in O(n) (no re-sorting). Print Emma Ulysses Solaris Dune Beloved.חיפוש בינארי: חיפוש במערך ממוין:binary search: Searching a sorted array:
המונחים של המפגשThis session's termsשני מצביעים: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני).two pointers: Two indexes moving over the array (from the ends inward, or one behind the other).
המונחים של המפגשThis session's termsBFS: חיפוש לרוחב:BFS: Breadth-first search:
המונחים של המפגשThis session's termsשיטה לשאלת ראיון: מבינים את השאלה ושואלים על מקרי קצה, פותרים קודם בדרך הפשוטה, אומרים את הסיבוכיות, משפרים (לרוב עם 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.
המונחים של המפגשThis session's termsבמערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני.In a sorted array, check the middle: if it's smaller than the target throw away the left half, if bigger the right.
חיפוש בינאריBinary searchבסימולציה רואים את lo, hi ו-mid מתכנסים.In the simulation you see lo, hi and mid close in.
חיפוש בינאריBinary searchשאלת הראיון הנפוצה ביותר: אילו שני מספרים מסתכמים ל-16?The most common interview question: which two numbers add up to 16?
Two Sum עם HashMapTwo Sum with a HashMapבמקום לבדוק כל זוג (O(n²)), זוכרים מה כבר ראינו ובודקים אם "המשלים" קיים: O(n).Instead of checking every pair (O(n²)), remember what you've seen and check whether the "complement" exists: O(n).
Two Sum עם HashMapTwo Sum with a HashMapכל איבר "נכנס" למקום שלו בחלק הממוין שמשמאלו, כמו קלפים ביד.Each item "slides" into place in the sorted part to its left, like cards in a hand.
מיון הכנסהInsertion sortO(n²) במקרה הרע, אבל מהיר על מערך כמעט ממוין.O(n²) at worst, but fast on an almost-sorted array.
מיון הכנסהInsertion sortב-Java אמיתית: Arrays.sort.In real Java: Arrays.sort.
מיון הכנסהInsertion sortמדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves
הצעד של היום בפרויקט מדףToday's step in the Shelf projectכמה צעדים לחיפוש בינארי במערך של מיליון?How many steps does a binary search take on a million items?
- כ-20About 20
- מיליוןA million
- 10001000
- 22
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כ-20About 20
נכון: log₂ של מיליון.Right: log₂ of a million.מה התנאי לחיפוש בינארי?What does a binary search require?
- מערך ממויןA sorted array
- מערך קטןA small array
- HashMapA HashMap
- מספרים חיובייםPositive numbers
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.מערך ממויןA sorted array
נכון.Right.בראיון קיבלתם שאלה. מה עושים קודם?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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.מוודאים שהבנתם ושואלים על מקרי קצהMake sure you understood and ask about edge cases
נכון.Right.חיפוש בינאריBinary search
- כתבו
find(sorted, target) שמחזירה אינדקס או -1.Write find(sorted, target) returning an index or -1.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.פלינדרום עם שני מצביעיםA palindrome with two pointers
isPalindrome(s) מתעלמת מאותיות גדולות, רווחים וסימנים, ומשווה מהקצוות פנימה.isPalindrome(s) ignores capitals, spaces and punctuation, and compares from the ends inward.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.התו הראשון שלא חוזרThe first non-repeating character
firstUnique(s) מחזירה את התו הראשון שמופיע פעם אחת (או -).firstUnique(s) returns the first character appearing once (or -).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מיון הכנסהInsertion sort
- כתבו את מיון ההכנסה מהתזכורת ומיינו
{9, 3, 7, 3, 1}:Write the insertion sort from the refresher and sort {9, 3, 7, 3, 1}:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves
- שני מדפים ממוינים לפי שנה.Two shelves sorted by year.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.פיבונאצ'י בלולאהFibonacci in a loop
fib(n) בלולאה עם שני משתנים, O(n).fib(n) in a loop with two variables, O(n).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.BFS: כמה צעדים בין ספריםBFS: how many steps between books
- מפה של "ספרים דומים".A map of "similar books".
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.while (lo <= hi): עם < מפספסים את האיבר האחרון.while (lo <= hi): with < you miss the last item.
חיפוש בינאריBinary searchreplaceAll("[^a-z]", "") אחרי toLowerCase.replaceAll("[^a-z]", "") after toLowerCase.
פלינדרום עם שני מצביעיםA palindrome with two pointersO(n): שני מעברים עדיין לינאריים.O(n): two passes are still linear.
התו הראשון שלא חוזרThe first non-repeating characterמה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.