Binary Search

Categories: Code Problem / Data Structures and Algorithms / Recursion (32) Code Problem / Data Structures and Algorithms / Binary Search (30) Code Problem / Data Structures and Algorithms / Linked Lists (26) Code Problem / Data Structures and Algorithms / Backtracking (26) Code Problem / Data Structures and Algorithms / Stacks and Queues (25) Code Problem / Data Structures and Algorithms / Hashing (24) Databases / SQL / Backup and Recovery (19) Code Problem / Data Structures and Algorithms / Arrays and Strings (19) Databases / SQL / Database Normalization (18) Code Problem / Data Structures and Algorithms / Time Complexity (17) Databases / SQL / Locking (16) Databases / SQL / Replication (15) Databases / SQL / SQL Queries (13) Code Problem / Data Structures and Algorithms / Graphs (13) Databases / SQL / Database Security (13)  More...

Recent solutions:

Check these solutions from our community:
Code Problem / Data Structures and Algorithms DifficultyMedium C#
Given a sorted array of integers and a value, this problem seeks to find the position of that value using binary search. For example, given the array [1, 3, 5, 7 , 9] and the value 5, the output would be 2.
About this solution: The candidate's solution does not demonstrate a level of completeness and does not solve the problem. The candidate's approach is not clear.
Nov 22
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This problem asks you to write a function that searches a sorted array of integers for a given value. If the value is found, the function should return the index where it is located. If the value is not found, the function should return -1.
About this solution: The candidate's solution is correct and demonstrates a level of completeness. The candidate has correctly identified that binary search is the optimal solution for this problem. The candidate's implementation of binary search is also correct. The only thing that could be improved is the candidate's explanation of the time and space complexity of the solution. The candidate correctly states that the time complexity is O(log n), but does not explain why. The candidate also correctly states that the space complexity is O(1), but again does not explain why.
Nov 21
Code Problem / Data Structures and Algorithms DifficultyMedium C#
The problem is to find the index of a given target value in a list of integers using the binary search algorithm. If the target value is not found in the list, the function should return -1.
About this solution: The candidate's solution correctly solves the problem. They use a for loop to iterate through the array and check if each element is equal to the target value. If it is, they set the index variable to the current index and break out of the loop. At the end, they print out the index variable. One improvement that could be made is to use the Array.IndexOf() method, which would make the code more concise.
Nov 19
Code Problem / Data Structures and Algorithms DifficultyMedium Python
The problem is to find the index of a given target value in a sorted array of integers. If the target value is not found in the array, the function should return -1.
About this solution: This solution is optimal because it is O(n) time complexity. However, it is not the most efficient solution. A more efficient solution would be to use a binary search, which would have a time complexity of O(log n).
Nov 18
Code Problem / Data Structures and Algorithms DifficultyMedium Python
The "Index of Target Number in List" code problem asks the reader to write a function that takes in a list of integers and a target number, and returns the index of the target number if it is present in the list, or -1 if it is not present.
About this solution: The candidate's solution is correct and uses a linear search algorithm, which is the most efficient way to search for an element in a list. The candidate's solution is complete and solves the problem.
Nov 18
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This technical problem deals with the binary search algorithm, specifically searching for a given key in a sorted array. The function should return the index of the key if it is found, or -1 if it is not found.
About this solution: The candidate's solution correctly implements a binary search algorithm to find the index of a given key in a sorted array. The solution is efficient and correctly returns the index of the key if it is present in the array, or -1 if it is not.
Nov 12
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This technical problem involves finding the index of a target value in a sorted array. If the target value is not present in the array, the function should return -1.
About this solution: The candidate's solution is correct and demonstrates a level of completeness. The candidate has correctly identified binary search as the optimal solution for this problem and has implemented it correctly. The candidate's approach is clear and easy to follow.
Nov 11
Code Problem / Data Structures and Algorithms DifficultyMedium Python
Given a list of integers and a target number, the function should return the index of the first number in the list that is larger than the target number. If there is no such number, the function should return -1.
About this solution: This solution is correct and demonstrates a good understanding of time and space complexity.
Nov 11