Skip to content

PreCourse-2: Exercise 1,2,3,4,5 Complete - #1897

Open
ankurgokhale05 wants to merge 2 commits into
super30admin:masterfrom
ankurgokhale05:master
Open

PreCourse-2: Exercise 1,2,3,4,5 Complete#1897
ankurgokhale05 wants to merge 2 commits into
super30admin:masterfrom
ankurgokhale05:master

Conversation

@ankurgokhale05

Copy link
Copy Markdown

PreCourse-2: Exercise 1,2,3,4,5 Complete

@super30admin

Copy link
Copy Markdown
Owner
  1. Correctness across all five exercises: The implementations of binary search, recursive quicksort, iterative quicksort, merge sort, and the slow/fast pointer middle-of-linked-list are all algorithmically correct. Each function handles the standard cases (element present, absent, first/last position, empty input) and the test suites you wrote cover these scenarios well.

  2. Time complexity: All five solutions hit the expected complexities — O(log n) for binary search, O(n log n) average for both quicksort variants, O(n log n) for merge sort, and O(n) for the linked-list middle finder. The median-of-three pivot selection in both quicksort implementations is a nice touch that reduces the probability of hitting the O(n²) worst case on already-sorted or reverse-sorted input.

  3. Space complexity: Binary search is O(1), the linked-list middle is O(1), and merge sort is O(n) auxiliary due to the sliced sublists. The recursive quicksort is O(log n) average / O(n) worst case for the call stack, and the iterative quicksort with the smaller-side-loop optimization correctly bounds the stack to O(log n) in the worst case — good awareness on your part.

  4. Code quality and structure: The code is clean, well-commented, and each file is self-contained with its own run_tests() / run_test() harness. Docstrings clearly state the approach, complexity, and LeetCode reference. Variable names are descriptive. One minor inconsistency: Exercise_4.py uses run_test() (singular) while the others use run_tests() — not a bug, just stylistic.

  5. Minor improvements / things to watch:

    • In Exercise_1.py, the function signature binarySearch(arr, l, r, x) accepts l and r but then ignores them and recomputes left = 0, right = len(arr) - 1. This works, but it's misleading — either use the passed-in bounds or drop them from the signature.
    • In Exercise_4.py, the merge sort creates new lists via slicing on every recursive call (left_half = arr[left:mid + 1]), which is O(n) extra space per level and defeats the standard in-place merge optimization. A more idiomatic version uses indices (l, r) and a single auxiliary buffer.
    • The printMiddle method in Exercise_3.py prints but doesn't return — fine for the original driver, but consider returning the value so the method is more reusable.
    • In Exercise_5.py, the iterative quicksort's smaller-side-loop optimization is correct, but the comment "O(log n) in the worst case" for space is slightly misleading — it's O(log n) worst case for the stack, which is what you meant, but worth being precise.

Overall, this is a solid, well-tested submission that demonstrates clear understanding of the algorithms and their trade-offs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants