חלקPart 7: Java בעולם האמיתיJava in the real world
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
אלגוריתמים, ראיון עבודה ופרויקט גמרAlgorithms, interviews & final project
סיבוכיות, חיפוש ומיון, שאלות ראיון נפוצות ב-Java, ותכנון פרויקט הגמר.
Complexity, searching and sorting, common Java interview questions, and planning the final project.
הנקודות החשובותKey points
- חיפוש בינארי: חיפוש במערך ממוין:binary search: Searching a sorted array:
- שני מצביעים: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני).two pointers: Two indexes moving over the array (from the ends inward, or one behind the other).
- במערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני.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 seelo,hiandmidclose 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.
- מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves
מונחיםTerms
- סיבוכיות (O גדול)complexity (big O): כמה העבודה גדלה כשהאוסף גדל.
containsב-ArrayListהוא O(n) (עובר על הכול), ב-HashSetO(1) בממוצע, וב-TreeSetO(log n).How the work grows as the collection grows.containson anArrayListis O(n) (it scans everything), on aHashSetO(1) on average, and on aTreeSetO(log n). - חיפוש בינאריbinary search: חיפוש במערך ממוין: בודקים את האמצע וזורקים חצי בכל צעד. O(log n): מיליון איברים בכ-20 צעדים.Searching a sorted array: check the middle and throw away half at each step. O(log n): a million items in about 20 steps.
- שני מצביעיםtwo pointers: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני). פותר הרבה שאלות ב-O(n) במקום O(n²).Two indexes moving over the array (from the ends inward, or one behind the other). Solves many questions in O(n) instead of O(n²).
- BFSBFS: חיפוש לרוחב: עוברים על גרף שכבה אחרי שכבה בעזרת תור. מוצא את המסלול הקצר ביותר (במספר צעדים).Breadth-first search: walk a graph layer by layer with a queue. Finds the shortest path (in number of steps).
- שיטה לשאלת ראיוןan interview method: מבינים את השאלה ושואלים על מקרי קצה, פותרים קודם בדרך הפשוטה, אומרים את הסיבוכיות, משפרים (לרוב עם HashMap או מיון), ובודקים על דוגמה.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.
- חיפוש בינאריbinary search: חיפוש במערך ממוין: בודקים את האמצע וזורקים חצי בכל צעד. O(log n): מיליון איברים בכ-20 צעדים.Searching a sorted array: check the middle and throw away half at each step. O(log n): a million items in about 20 steps.
- שני מצביעיםtwo pointers: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני). פותר הרבה שאלות ב-O(n) במקום O(n²).Two indexes moving over the array (from the ends inward, or one behind the other). Solves many questions in O(n) instead of O(n²).
- BFSBFS: חיפוש לרוחב: עוברים על גרף שכבה אחרי שכבה בעזרת תור. מוצא את המסלול הקצר ביותר (במספר צעדים).Breadth-first search: walk a graph layer by layer with a queue. Finds the shortest path (in number of steps).
- שיטה לשאלת ראיוןan interview method: מבינים את השאלה ושואלים על מקרי קצה, פותרים קודם בדרך הפשוטה, אומרים את הסיבוכיות, משפרים (לרוב עם HashMap או מיון), ובודקים על דוגמה.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.
Cheatsheet
חיפוש בינאריBinary search
int[] years = {1815, 1817, 1922, 1949, 1965, 2004};
System.out.println(find(years, 1949) + " " + find(years, 2000));Two Sum עם HashMapTwo Sum with a HashMap
System.out.println(Arrays.toString(twoSum(new int[]{3, 8, 11, 5}, 16)));מיון הכנסהInsertion sort
int[] a = {5, 2, 4, 1};
sort(a);
System.out.println(Arrays.toString(a));שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|