אלגוריתמים, ראיון עבודה ופרויקט גמרAlgorithms, interviews & final project
סיבוכיות, חיפוש ומיון, שאלות ראיון נפוצות ב-Java, ותכנון פרויקט הגמר.
Complexity, searching and sorting, common Java interview questions, and planning the final project.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
חיפוש בינאריBinary search
כתבו find(sorted, target) שמחזירה אינדקס או -1. עבור השנים 1815…2004 הדפיסו 0 5 -1 (הראשון, האחרון, ו-1900 שלא קיים).
Write find(sorted, target) returning an index or -1. For the years 1815…2004 print 0 5 -1 (the first, the last, and 1900 which isn't there).
איך הפלט אמור להיראות?What should the output look like?
0 5 -1
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:while (lo <= hi): עם < מפספסים את האיבר האחרון.while (lo <= hi): with < you miss the last item.
פלינדרום עם שני מצביעיםA palindrome with two pointers
isPalindrome(s) מתעלמת מאותיות גדולות, רווחים וסימנים, ומשווה מהקצוות פנימה. הדפיסו true false עבור Never odd or even ו-Java.
isPalindrome(s) ignores capitals, spaces and punctuation, and compares from the ends inward. Print true false for Never odd or even and Java.
איך הפלט אמור להיראות?What should the output look like?
true false
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:replaceAll("[^a-z]", "") אחרי toLowerCase.replaceAll("[^a-z]", "") after toLowerCase.
התו הראשון שלא חוזרThe first non-repeating character
firstUnique(s) מחזירה את התו הראשון שמופיע פעם אחת (או -). שני מעברים: ספירה, ואז חיפוש. הדפיסו w - עבור swiss ו-aabb.
firstUnique(s) returns the first character appearing once (or -). Two passes: count, then search. Print w - for swiss and aabb.
איך הפלט אמור להיראות?What should the output look like?
w -
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:O(n): שני מעברים עדיין לינאריים.O(n): two passes are still linear.
Write the insertion sort from the refresher and sort {9, 3, 7, 3, 1}: [1, 3, 3, 7, 9].
איך הפלט אמור להיראות?What should the output look like?
[1, 3, 3, 7, 9]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:מזיזים ימינה כל עוד a[j] > x.Shift right while a[j] > x.
מדף, צעד 42: מיזוג שני מדפים ממויניםShelf, step 42: merging two sorted shelves
פרויקט מלווהCompanion 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.
איך הפלט אמור להיראות?What should the output look like?
Emma Ulysses Solaris Dune Beloved
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:שני מצביעים: לוקחים את הקטן מבין שני הראשונים.Two pointers: take the smaller of the two heads.
fib(n) בלולאה עם שני משתנים, O(n). הגרסה הרקורסיבית הפשוטה היא O(2ⁿ) ואיטית מאוד ב-30. הדפיסו 55 832040.
fib(n) in a loop with two variables, O(n). The plain recursive version is O(2ⁿ) and very slow at 30. Print 55 832040.
איך הפלט אמור להיראות?What should the output look like?
55 832040
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:t = a + b; a = b; b = t;t = a + b; a = b; b = t;
BFS: כמה צעדים בין ספריםBFS: how many steps between books
מפה של "ספרים דומים". steps(links, from, to) מוצאת במספר הצעדים הקטן ביותר עם BFS (תור ומפת מרחקים), או -1. הדפיסו 2 -1 (Dune → Solaris → Roadside Picnic).
A map of "similar books". steps(links, from, to) finds the fewest steps with BFS (a queue and a distance map), or -1. Print 2 -1 (Dune → Solaris → Roadside Picnic).
איך הפלט אמור להיראות?What should the output look like?
2 -1
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:מסמנים מרחק כשמכניסים לתור, כדי לא לבקר פעמיים.Mark the distance when adding to the queue, so nothing is visited twice.