Add lesson 9: Mock interview
3 changed files with 41 additions and 0 deletions
lessons/09-mock-interview/README.md ADDED
| @@ -0,0 +1,19 @@ | ||
| 1 | +# Lesson 9: Mock interview | |
| 2 | + | |
| 3 | +We look at mock interview the way working developers use it, with examples you can change and run right away. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/algorithms/lessons/9](https://glibcode.net/courses/algorithms/lessons/9) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `mock_interview.py`: a practice question with a first and a better answer | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +python3 mock_interview.py | |
| 15 | +``` | |
| 16 | + | |
| 17 | +## Exercises | |
| 18 | + | |
| 19 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/09-mock-interview/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 9 | |
| 2 | + | |
| 3 | +1. Time yourself: rewrite the example from memory in 10 minutes, then diff it against the original. | |
| 4 | +2. What happens with an empty input? Change the code so it handles that case with a clear message. | |
| 5 | +3. Rewrite the main part so it runs twice with two different inputs, without copying the code. | |
lessons/09-mock-interview/mock_interview.py ADDED
| @@ -0,0 +1,17 @@ | ||
| 1 | +"""Question: given a list of meeting times, can one person attend them all?""" | |
| 2 | + | |
| 3 | + | |
| 4 | +def can_attend_naive(meetings): # O(n^2): compare every pair | |
| 5 | + for i, (s1, e1) in enumerate(meetings): | |
| 6 | + for s2, e2 in meetings[i + 1:]: | |
| 7 | + if s1 < e2 and s2 < e1: | |
| 8 | + return False | |
| 9 | + return True | |
| 10 | + | |
| 11 | + | |
| 12 | +def can_attend(meetings): # O(n log n): sort, then check neighbours | |
| 13 | + meetings = sorted(meetings) | |
| 14 | + return all(prev[1] <= cur[0] for prev, cur in zip(meetings, meetings[1:])) | |
| 15 | + | |
| 16 | + | |
| 17 | +print(can_attend([(9, 10), (10, 11), (13, 14)]), can_attend([(9, 12), (11, 13)])) | |