אלגוריתמים, ראיון עבודה ופרויקט גמרAlgorithms, interviews & final project
סיבוכיות, חיפוש ומיון, שאלות ראיון נפוצות ב-Java, ותכנון פרויקט הגמר.
Complexity, searching and sorting, common Java interview questions, and planning the final project.
01המונחים של המפגשThis session's terms
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.
Before the code, the words. Every term here is also linked in the course glossary.
- חיפוש בינארי: חיפוש במערך ממוין: בודקים את האמצע וזורקים חצי בכל צעד. O(log n): מיליון איברים בכ-20 צעדים.binary search: Searching a sorted array: check the middle and throw away half at each step. O(log n): a million items in about 20 steps.
- שני מצביעים: שני אינדקסים שזזים על המערך (מהקצוות פנימה, או אחד אחרי השני). פותר הרבה שאלות ב-O(n) במקום O(n²).two pointers: 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²).
- BFS: חיפוש לרוחב: עוברים על גרף שכבה אחרי שכבה בעזרת תור. מוצא את המסלול הקצר ביותר (במספר צעדים).BFS: Breadth-first search: walk a graph layer by layer with a queue. Finds the shortest path (in number of steps).
- שיטה לשאלת ראיון: מבינים את השאלה ושואלים על מקרי קצה, פותרים קודם בדרך הפשוטה, אומרים את הסיבוכיות, משפרים (לרוב עם 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.
02חיפוש בינאריBinary search
במערך ממוין בודקים את האמצע: אם קטן מהמבוקש זורקים את החצי השמאלי, ואם גדול את הימני. בסימולציה רואים את 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.
public class Main {
static int find(int[] sorted, int target) {
int lo = 0;
int hi = sorted.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (sorted[mid] == target) {
return mid;
} else if (sorted[mid] < target) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] years = {1815, 1817, 1922, 1949, 1965, 2004};
System.out.println(find(years, 1949) + " " + find(years, 2000));
}
}3 -1
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
03Two Sum עם HashMapTwo Sum with a HashMap
שאלת הראיון הנפוצה ביותר: אילו שני מספרים מסתכמים ל-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).
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main {
static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> seen = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
Integer j = seen.get(target - nums[i]);
if (j != null) {
return new int[]{j, i};
}
seen.put(nums[i], i);
}
return new int[0];
}
public static void main(String[] args) {
System.out.println(Arrays.toString(twoSum(new int[]{3, 8, 11, 5}, 16)));
}
}[2, 3]
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
04מיון הכנסהInsertion 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.
import java.util.Arrays;
public class Main {
static void sort(int[] a) {
for (int i = 1; i < a.length; i++) {
int x = a[i];
int j = i - 1;
while (j >= 0 && a[j] > x) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = x;
}
}
public static void main(String[] args) {
int[] a = {5, 2, 4, 1};
sort(a);
System.out.println(Arrays.toString(a));
}
}[1, 2, 4, 5]
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
מה קורה עכשיוWhat happens now
פלטOutput
Stack: משתנים בכל מתודהStack: each method's variables
Heap: העצמיםHeap: the objects
05הצעד של היום בפרויקט מדףToday's step in the Shelf project
שני מדפים ממוינים לפי שנה. 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.
את הצעד הזה כותבים בעמוד התרגול (התרגיל המסומן כצעד בפרויקט). בעמוד הפתרונות יש פתרון מלא עם סימולציה שמריצה אותו.
You write this step on the practice page (the exercise marked as a project step). The solutions page has a full solution with a simulation that runs it.
סיימתם את ההרצאה!Lecture complete!
עכשיו המצגת לחזרה מהירה, או ישר לתרגול.
Next: the slides for a quick review, or straight to practice.