Backtracking

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++
This problem is about finding the shortest path through a 2D array of black and white pixels. The input is a 2D array of 0s and 1s, and the output is the length of the shortest path from the top left to the bottom right.
About this solution: The candidate's solution is correct and solves the problem. The candidate uses a breadth-first search approach, which is a good approach for this problem. The candidate's code is well-written and easy to understand.
Nov 26
Code Problem / Data Structures and Algorithms DifficultyMedium JavaScript
Given a set of distinct integers, this code prints the size of the largest subset where the sum of any two numbers is not evenly divisible by a given number.
About this solution: The candidate's solution is correct and demonstrates a level of completeness. The approach is also sensible.
Nov 25
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This problem is about finding all possible permutations of a set of distinct integers. For example, if the input is [1, 2, 3], then the output should be [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]].
About this solution: The candidate's solution is complete and solves the problem. The approach is recursive and uses a backtracking algorithm. The time and space complexity of the solution is O(n!).
Nov 24
Code Problem / Data Structures and Algorithms DifficultyMedium Python
Given a 2D array, this problem seeks to find the length of the longest path of consecutive numbers that can be formed starting from any cell. The path can move in any of the 8 directions.
About this solution: The candidate's solution is correct and solves the problem. The candidate's approach is to use a depth first search to find the longest path starting from each cell. This is a good approach. The candidate's solution is optimal because it only needs to search each cell once.
Nov 24
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This function returns true if the given 2D array is a valid Sudoku puzzle, and false otherwise. A valid Sudoku puzzle is one in which each row, column, and 3x 3 sub-array contains all of the integers from 1 to 9 with no repeats.
About this solution: This is a great solution! The candidate has thought about the problem and broken it down into smaller pieces that are easier to solve. The candidate has also written helper functions to make the code more readable.
Nov 24
Code Problem / Data Structures and Algorithms DifficultyMedium Python
Given a 2D array of integers, this problem looks for the longest path of consecutive numbers that can be formed in the array. The path can start and end at any location in the array , and the example output given is 6.
About this solution: The candidate's solution is correct and uses a DFS algorithm to find the longest path. The time complexity is O(n) where n is the number of elements in the array. The space complexity is O(n) where n is the number of elements in the array.
Nov 23
Code Problem / Data Structures and Algorithms DifficultyMedium C#
This problem involves finding all possible subsets of distinct integers. The solution set must not contain duplicate subsets, and the elements in each subset must be in non-descending order.
About this solution: The candidate's solution correctly generates all possible subsets for a given set of distinct integers. The candidate uses backtracking to generate the subsets. The candidate could improve their solution by adding comments to explain the backtracking approach and how it generates all possible subsets. The candidate could also improve the naming of their variables to make the code more readable.
Nov 21
Code Problem / Data Structures and Algorithms DifficultyMedium Python
This problem asks to return all possible subsets of a given set of distinct integers. For example, for the input [1,2,3], the output would be all possible subsets of those integers: [1], [2], [3], [1,2], [1,3], [2,3], and [1,2,3].
About this solution: The candidate's solution is correct and solves the problem. The approach is to use a recursive function that takes in a list of integers and a list of lists. The function will iterate through the list of integers and add each integer to the list of lists. Then it will call itself with the list of integers minus the current integer and the list of lists. This will continue until the list of integers is empty. The function will then return the list of lists. This solution is optimal because it is O(2^n) time complexity and O(n) space complexity.
Nov 17