avatarNaina Chaturvedi

Summary

The provided content outlines a comprehensive guide on binary search, including its importance, implementation, patterns, techniques, and solutions to common problems, alongside additional resources for system design and data science projects.

Abstract

The web content titled "Day 16 of 30 days of Data Structures and Algorithms and System Design Simplified — Binary Search" delves into the binary search algorithm, explaining its significance in efficiently searching sorted arrays by dividing the search interval in half with each step. The article covers the fundamental principles of binary search, its iterative and recursive implementations, and essential patterns and techniques to solve related problems. It also provides detailed solutions to key binary search questions, such as searching in a 2D matrix and finding the insert position for a target value. Additionally, the content includes a complexity analysis of binary search, tips for solving binary search questions quickly, and a preview of the next topic in the series, hashing. The post concludes with a collection of other educational series and newsletters, inviting readers to subscribe for more insights and updates in the field of technology, data science, and machine learning.

Opinions

  • The author emphasizes the high importance of mastering binary search due to its efficiency and frequent appearance in technical interviews.
  • Binary search is presented as a foundational technique with a "very high" importance level, suggesting that it is crucial for anyone in the field of software development and algorithms.
  • The article advocates for learning by doing, encouraging readers to implement binary search algorithms and solve problems to gain a deeper understanding.
  • The author believes in the value of consistent practice, as evidenced by the daily addition of new binary search questions and solutions.
  • There is an emphasis on the practical application of binary search, with examples and patterns that extend beyond theoretical knowledge.
  • The content promotes a community-driven approach to learning, with invitations to subscribe to a YouTube channel and newsletter for continued education and engagement with a broader audience.
  • The author suggests that understanding complexity analysis is essential for optimizing binary search algorithms and appreciating their performance benefits.

Day 16 of 30 days of Data Structures and Algorithms and System Design Simplified — Binary Search

Pic credits : Hackernoon

Welcome back peeps. Hope all’s well. In this post we will cover Binary Search as follows —

What and Why Binary Search (in 2–3 sentences)?

How does Binary Search work?

Important Patterns and Techniques in Binary Search Questions

Most Important Questions with Solutions

Complexity Analysis

Tips and Techniques to solve Binary Search Questions Fast.

Projects Videos —

All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).

Subscribe today!

System Design Case Studies — In Depth

Design Instagram

Design Messenger App

Design Twitter

Design URL Shortener

Design Dropbox

Design Youtube

Design API Rate Limiter

Design Web Crawler

Design Facebook’s Newsfeed

Design Yelp

Design Uber

Design Tinder

Design Tiktok

Design Whatsapp

Most Popular System Design Questions

Mega Compilation : Solved System Design Case studies

Binary Search

Importance : Very High

Day 2 of data structures and algorithms covers the topics that are most important and with highest ROI.

Note : New Binary Search questions with solutions are added everyday. So keep checking this post daily.

Let’s dive in!

What is Binary Search?

Binary search is a very efficient search technique in which the searching follows divide and conquer strategy on the sorted arrays.

It reduces the search space by half with every iteration which makes it very efficient algorithm as compared to the linear search when it comes to searching in huge amount of data.

Pic credits : Hackernoon

Examples of Binary Search problems —

Search element in a circular sorted array

Search a 2D Matrix

Search in rotated sorted array

Find Peak elemnt

Binary Search Tree traversals

Subarray Sum

Median of two sorted array etc

How does Binary Search work?

Binary search works on the principle of divide and conquer.

Binary Search is efficient as compared to linear search as it reduces the number of elements to be checked by half with each comparison.

Binary search works by repeatedly dividing the array in half, until the target element is found or it is determined that the element does not exist in the array. The algorithm starts by considering the middle element of the array and comparing it to the target element. If the middle element is equal to the target element, the algorithm returns the index of the middle element. If the middle element is less than the target element, the algorithm continues the search in the upper half of the array. If the middle element is greater than the target element, the algorithm continues the search in the lower half of the array. The search continues until the target element is found or the search interval is empty.

The given array is sorted and the two pointers left and right which are positioned at the beginning ( index 0 ) or end ( last index)of the array respectively.

Once positioned, calculate the mid = left + right/2

The mid will point to the mid element of the array. If the element at the mid is the target element then return the mid index

If the mid element is not the target element, then if the target element is smaller than mid element, shift the right pointer to the mid-1 index and again iterate from step 1. The remaining right array from the mid will be discarded.

If the mid element is not the target element, then if the target element is greater than mid element, shift the left pointer to the mid+1 index and again iterate from step 1. The remaining left array to the mid will be discarded.

Pic credits : geekycodes

Discarding the array based on the condition improves the time complexity.

There are two ways in which binary search algorithm can be implemented —

Iterative approach

Recursive Approach

Important Patterns and Techniques in Binary Search Questions

Some important patterns and techniques in binary search questions include:

  1. Divide and conquer: This technique is used to break down a problem into smaller subproblems, and is often used to search for a specific element or value in an array.
  2. Recursion: This technique is used to solve problems by breaking them down into smaller subproblems, and is often used to implement a binary search algorithm.
  3. Iteration: This technique is used to solve problems by repeatedly applying a specific operation, and is often used to implement a binary search algorithm.
  4. Lower and upper bound: This technique is used to find the first and last occurrence of a specific element in an array, and it can be used to solve many different types of problems.
  5. Time and space complexity: It’s important to understand the time and space complexity of a binary search algorithm and how it can be optimized for specific use cases.

Binary Tree questions have patterns such as —

  1. Array is sorted
  2. Tree is BST ( search in a BST)
  3. Min and Max array
  4. Intersection of two arrays

In the iterative Binary Search, the iterations for the binary search is done using loops

In recursive, the binary search function is called recursively.

Patterns → Questions like below belong to Binary Search( not limited to):

Intersection of two arrays

Split array Largest sum

Find Right Interval

Search element in a circular sorted array

Search a 2D Matrix

Search in rotated sorted array

Find Peak elemnt

Binary Search Tree traversals

Subarray Sum

Median of two sorted array etc

Most Important Questions with Solutions

Note : New Binary Search questions with solutions are added everyday. So keep checking this post daily.

Golden rule is — Learn by doing/implementing

In this we will see most important Binary Search questions.

Search a 2D Matrix

Question —

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

  • Integers in each row are sorted from left to right.
  • The first integer of each row is greater than the last integer of the previous row.

Example :

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Solution :

Main Logic/Idea —

The main logic is to use binary search to identify which row and column to search in for the target element. Iterate through top and bottom and left and right to calculate mid and increment/decrement the pointers accordingly in order to efficiently implement the binary search on the 2D matrix.

Implementation —

def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        row,col = len(matrix),len(matrix[0])
        top,bottom =0,row-1
        
        while top <= bottom:
            row = (top + bottom)//2
            if target > matrix[row][-1]:
                top = row +1
            elif target < matrix[row][0]:
                bottom = row-1
            else:
                break
                
        if not ( top <= bottom):
            return False
        
        row = (top+bottom)//2
        left,right = 0,col-1
        while left <= right:
            mid = (left+right)//2
            if target > matrix[row][mid]:
                left = mid+1
            elif target < matrix[row][mid]:
                right = mid -1
            else:
                return True
        return False

Question Link

Similar Pattern —

Search a 2D Matrix II

Full Code Video Explanation ( In progress. Subscribe today for updates) :

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Binary Search

Question —

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4

Solution :

Main Logic/Idea —

The given array is sorted and the two pointers left and right which are positioned at the beginning ( index 0 ) or end ( last index)of the array respectively. Once positioned, calculate the mid = left + right/2

The mid will point to the mid element of the array. If the element at the mid is the target element then return the mid index.

If the mid element is not the target element, then if the target element is smaller than mid element, shift the right pointer to the mid-1 index and again iterate from step 1. The remaining right array from the mid will be discarded.

If the mid element is not the target element, then if the target element is greater than mid element, shift the left pointer to the mid+1 index and again iterate from step 1. The remaining left array to the mid will be discarded.

Implementation —

def search(self, nums: List[int], target: int) -> int:
        left, right =0,len(nums) -1
        
        while left <= right:
            m = (left+right)//2
            if target < nums[m]:
                right = m -1
            elif target > nums[m]:
                left = m +1
                
            else:
                return m
        return -1

Question Link

Similar Pattern —

Full Code Video Explanation ( In progress. Subscribe today for updates) :

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Search Insert Position

Question —

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example :

Input: nums = [1,3,5,6], target = 5
Output: 2

Solution :

Main Logic/Idea —

Use above logic ( as pervious question) to find the index to insert the target element can be inserted.

Implementation —

def searchInsert(self, nums: List[int], target: int) -> int:
        left, right = 0, len(nums)-1
       
        while left <= right:
            mid = (left+right)//2
            if target == nums[mid]:
                return mid
            elif target > nums[mid]:
                left = mid+1
            else:
                right = mid -1
        return left

Question Link

Similar Pattern —

Average Waiting Time

Maximum Earnings From Taxi

Profitable Schemes

Full Code Video Explanation ( In progress. Subscribe today for updates) :

Note : New Binary Search questions with solutions are added everyday. So keep checking this post daily.

Complexity Analysis

Read complexity analysis post before calculating binary search complexity.

Since the search space is divided by 2 in every iteration of the array :

Best Case time complexity : O(1)

Average Case time complexity : O(logn)

Worst Case time complexity : O(logn)

Tips and Techniques to solve Binary Search Questions Fast.

Binary search is used where every its given —

Find the solution in log n time

Array is sorted

Questions that asks for average time

Binary Search Tree traversals and search for node in BST

Min and Max Array

Sorted Sub array sum.

That’s it for now. Day 17 : Hashing coming soon !

Let me know if you have questions in the comment section below. Subscribe/ Follow, Like/Clap as it will encourage me to write more in my free time and Stay Tuned!!

Read More —

11 most important System Design Base Concepts

1. System design basics

2. Horizontal and vertical scaling

3. Load balancing and Message queues

4. High level design and low level design, Consistent Hashing, Monolithic and Microservices architecture

5. Caching, Indexing, Proxies

6. Networking, How Browsers work, Content Network Delivery ( CDN)

7. Database Sharding, CAP Theorem, Database schema Design

8. Concurrency, API, Components + OOP + Abstraction

9. Estimation and Planning, Performance

10. Map Reduce, Patterns and Microservices

11. SQL vs NoSQL and Cloud

12. Most Popular System Design Questions

13. System Design Template — How to solve any System Design Question

14. Quick RoundUp : Solved System Design Case Studies

Some of the other best Series —

60 days of Data Science and ML Series with projects

30 Days of Natural Language Processing ( NLP) Series

30 days of Machine Learning Ops

30 days of Data Structures and Algorithms and System Design Simplified

60 Days of Deep Learning with Projects Series

30 days of Data Engineering with projects Series

Data Science and Machine Learning Research ( papers) Simplified **

100 days : Your Data Science and Machine Learning Degree Series with projects

23 Data Science Techniques You Should Know

Tech Interview Series — Curated List of coding questions

Complete System Design with most popular Questions Series

Complete Data Visualization and Pre-processing Series with projects

Complete Python Series with Projects

Complete Advanced Python Series with Projects

Kaggle Best Notebooks that will teach you the most

Complete Developers Guide to Git

Exceptional Github Repos — Part 1

Exceptional Github Repos — Part 2

All the Data Science and Machine Learning Resources

210 Machine Learning Projects

Tech Newsletter —

If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :

For Python Projects —

For complete 60 days of Data Science and ML : Day 1 — Day 60 : Quick Recap of 60 days of Data Science and ML

Follow for more updates. Stay tuned and keep coding!

For other projects, tune to —

Build Machine Learning Pipelines( With Code)

Recurrent Neural Network with Keras

Clustering Geolocation Data in Python using DBSCAN and K-Means

Facial Expression Recognition using Keras

Hyperparameter Tuning with Keras Tuner

Custom Layers in Keras

Programming
Software Development
Tech
Data Science
Machine Learning
Recommended from ReadMedium