Skip to content

🚧 #3340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

🚧 #3340

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,28 @@ Welcome to our open-source content repository! At Enki, we feel that education i
Learning happens best in a psychologically safe environment, so we've adopted the [contributor covenant](https://www.contributor-covenant.org/) as our code of conduct. If a list of rules isn't helping you envision what we're after, [here's another great resource](https://www.recurse.com/manual#sec-environment) (from The Recurse Center, read the Social Rules section) for how to participate in creating a community you'd like to be a part of. We _will_ enforce this, please get in touch with [email protected] with questions or concerns. **Everyone** is learning all the time, so disputes will be arbitrated first, and good-faith efforts are what is expected.

This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>. In simpler terms, it means you can't charge other people to use it, or use it to make money, but you can definitely use it to help people learn and you're free to remix or adapt what we have, so long as you attribute Enki, and the author of the work.

- interview-patterns-binary-search-basic
- interview-patterns-binary-search-basic-exercise
- interview-patterns-binary-search-modified
- interview-patterns-binary-search-modified-exercise
- interview-patterns-two-pointers-basic
- interview-patterns-two-pointers-basic-exercise
- interview-patterns-fast-slow-basic
- interview-patterns-fast-slow-basic-exercise
- interview-patterns-merge-intervals-basic
- interview-patterns-merge-intervals-basic-exercise
- interview-patterns-cyclic-sort-basic
- interview-patterns-cyclic-sort-basic-exercise
- interview-patterns-inplace-reversal-basic
- interview-patterns-inplace-reversal-basic-exercise
- interview-patterns-tree-bfs-basic
- interview-patterns-tree-bfs-basic-exercise
- interview-patterns-tree-dfs-basic
- interview-patterns-tree-dfs-basic-exercise
- interview-patterns-subsets-basic
- interview-patterns-subsets-basic-exercise
- interview-patterns-top-k-basic
- interview-patterns-top-k-basic-exercise
- interview-patterns-k-way-merge-basic
- interview-patterns-k-way-merge-basic-exercise
46 changes: 46 additions & 0 deletions tech-interviews/interview-patterns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Interview Patterns
description: Master common algorithmic patterns for technical interviews

sections:
'0':
# Core Patterns
- interview-patterns-two-pointers
- interview-patterns-two-pointers-exercise
- interview-patterns-two-pointers-fast-slow
- interview-patterns-two-pointers-fast-slow-exercise
- interview-patterns-two-pointers-three
- interview-patterns-two-pointers-three-exercise
- interview-patterns-two-pointers-merge
- interview-patterns-two-pointers-merge-exercise
- interview-patterns-two-pointers-merge-inplace
- interview-patterns-two-pointers-merge-inplace-exercise
- interview-patterns-two-pointers-intersection
- interview-patterns-two-pointers-intersection-exercise
- interview-patterns-two-pointers-intersection-three
- interview-patterns-two-pointers-intersection-three-exercise
- interview-patterns-two-pointers-partition
- interview-patterns-two-pointers-partition-exercise
- interview-patterns-two-pointers-partition-two
- interview-patterns-two-pointers-partition-two-exercise
- interview-patterns-two-pointers-partition-three
- interview-patterns-two-pointers-partition-three-exercise
- interview-patterns-sliding-window-fixed
- interview-patterns-sliding-window-fixed-exercise
- interview-patterns-sliding-window-variable
- interview-patterns-sliding-window-variable-exercise
- interview-patterns-sliding-window-string
- interview-patterns-sliding-window-string-exercise
- interview-patterns-binary-search-basic
- interview-patterns-binary-search-basic-exercise
- interview-patterns-binary-search
- interview-patterns-binary-search-exercise

# Advanced Patterns
- interview-patterns-dynamic-programming
- interview-patterns-dynamic-programming-exercise
- interview-patterns-substring
- interview-patterns-substring-exercise
- interview-patterns-backtracking
- interview-patterns-backtracking-exercise
- interview-patterns-graph
- interview-patterns-graph-exercise
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: Basic Binary Search Exercise
description: Practice implementing binary search on sorted arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
author: enki-ai
type: normal
category: coding
---

# Basic Binary Search Practice

Implement binary search to find a target element in a sorted array:

```python
def binary_search(nums, target):
"""
Args:
nums: Sorted array of integers
target: Element to find
Returns:
Index of target if found, -1 otherwise
Example:
binary_search([1,2,3,4,5], 3) -> 2
binary_search([1,2,3,4,5], 6) -> -1
"""
# Your code here
pass

# Test cases
assert binary_search([1,2,3,4,5], 3) == 2
assert binary_search([1,2,3,4,5], 6) == -1
assert binary_search([1], 1) == 0
assert binary_search([], 1) == -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: Basic Binary Search
description: Learn to efficiently search sorted arrays using binary search
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
author: enki-ai
type: normal
category: must-know
practiceQuestion:
formats:
- fill-in-the-gap
context: standalone
revisionQuestion:
formats:
- fill-in-the-gap
context: standalone
---

# Basic Binary Search

Binary search finds an element in a sorted array by repeatedly dividing the search space in half.

```python
def binary_search(nums, target):
left = 0
right = len(nums) - 1

while left <= right:
mid = (left + right) // 2

if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1

return -1 # Not found
```

> 💡 The array must be sorted for binary search to work correctly.

---
# Practice

Complete the binary search function:

```python
def binary_search(arr, target):
left = ???
right = ???

while left <= right:
mid = (left + right) // ???

if arr[mid] == ???:
return ???
elif arr[mid] < target:
left = ???
else:
right = ???
```

- `0`
- `len(arr) - 1`
- `2`
- `target`
- `mid`
- `mid + 1`
- `mid - 1`

---
# Revision

Binary search has a time complexity of:

???

- O(log n)
- O(n)
- O(n log n)
- O(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: Modified Binary Search Exercise
description: Practice finding insertion points using binary search
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
author: enki-ai
type: normal
category: coding
---

# Finding Insertion Points Practice

Find the index where a target should be inserted in a sorted array:

```python
def search_insert(nums, target):
"""
Args:
nums: Sorted array of integers
target: Number to insert
Returns:
Index where target should be inserted
Example:
search_insert([1,3,5,6], 5) -> 2
search_insert([1,3,5,6], 2) -> 1
"""
# Your code here
pass

# Test cases
assert search_insert([1,3,5,6], 5) == 2
assert search_insert([1,3,5,6], 2) == 1
assert search_insert([1,3,5,6], 7) == 4
assert search_insert([1,3,5,6], 0) == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
author: enki-ai
type: normal
category: coding
---

# Search in Rotated Array

Implement a function that searches for a target value in a sorted array that has been rotated at some pivot point.

```python
def search_rotated(nums, target):
"""
Search for target in a rotated sorted array.

Args:
nums: List[int], sorted array rotated at some pivot
target: int, value to find

Returns:
int: index of target if found, -1 otherwise

Example:
>>> search_rotated([4,5,6,7,0,1,2], 0)
4
>>> search_rotated([4,5,6,7,0,1,2], 3)
-1
"""
if not nums:
return -1

left = 0
right = len(nums) - 1

while left <= right:
mid = (left + right) // 2

if nums[mid] == target:
return mid

# Check which half is sorted
if nums[left] <= nums[mid]:
# Left half is sorted
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
# Right half is sorted
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1

return -1

# Tests
def test_search_rotated():
# Test empty array
assert search_rotated([], 5) == -1

# Test array with one element
assert search_rotated([1], 1) == 0
assert search_rotated([1], 2) == -1

# Test normal cases
assert search_rotated([4,5,6,7,0,1,2], 0) == 4
assert search_rotated([4,5,6,7,0,1,2], 3) == -1
assert search_rotated([1,3], 3) == 1

# Test edge cases
assert search_rotated([3,1], 1) == 1
assert search_rotated([5,1,3], 3) == 2

print("All tests passed!")

test_search_rotated()
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
author: enki-ai
type: normal
category: coding
---

# Search in Rotated Sorted Array

Implement a function that searches for a target value in a rotated sorted array.

```python
def search_rotated_array(nums, target):
"""
Search for target in rotated sorted array.

Args:
nums: List[int], rotated sorted array (no duplicates)
target: int, value to search for

Returns:
int: Index of target if found, -1 otherwise

Example:
>>> search_rotated_array([4,5,6,7,0,1,2], 0)
4
>>> search_rotated_array([4,5,6,7,0,1,2], 3)
-1
"""
# Your code here
pass

def test_search_rotated_array():
# Test 1: Normal rotated array
assert search_rotated_array([4,5,6,7,0,1,2], 0) == 4
assert search_rotated_array([4,5,6,7,0,1,2], 3) == -1

# Test 2: Array rotated at different positions
assert search_rotated_array([5,6,7,0,1,2,4], 5) == 0
assert search_rotated_array([2,4,5,6,7,0,1], 0) == 5

# Test 3: Not rotated array
assert search_rotated_array([1,2,3,4,5,6,7], 4) == 3

# Test 4: Single element array
assert search_rotated_array([1], 1) == 0
assert search_rotated_array([1], 0) == -1

# Test 5: Two element array
assert search_rotated_array([1,2], 1) == 0
assert search_rotated_array([2,1], 1) == 1

# Test 6: Empty array
assert search_rotated_array([], 5) == -1

# Test 7: Target at rotation point
assert search_rotated_array([3,4,5,1,2], 1) == 3
assert search_rotated_array([3,4,5,1,2], 5) == 2

# Test 8: Negative numbers
assert search_rotated_array([-3,-2,0,-7,-5,-4], -4) == 5

print("All test cases passed!")

# Run tests
if __name__ == "__main__":
test_search_rotated_array()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: Modified Binary Search
description: Learn variations of binary search for special cases
Loading