Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

All posts tagged algorithms

Spiral Matrix

Problem Statement Given an m x n matrix, return all elements of the matrix in spiral order. Example 1 Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2 Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Solution class Solution(object): def spiralOrder(self, matrix): “”” :type matrix: List[List[int]] :rtype: List[int] “”” result = [] R . . . Read more

Insert Interval

Problem Statement Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1 Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]] Example 2 Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval . . . Read more

Gray Code

Problem Statement The gray code is a binary numeral system where two successive values differ in only one bit. Given an integer n representing the total number of bits in the code, return any sequence of gray code. A gray code sequence must begin with 0. Example 1 Input: n . . . Read more

Jump Game II

Problem Statement Given an array of non-negative integers , you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps. You can assume . . . Read more

Max Array Sum with Non-adjacent Elements

Problem Statement Given an array of integers, find the subset of non-adjacent elements with the maximum sum. Calculate the sum of that subset. It is possible that the maximum sum is 0, the case when all elements are negative. Example The following subsets with more than 1 element exist. These exclude . . . Read more

Longest Common Prefix

Problem Statement Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Examples Solution

Valid Parentheses

Problem Statement Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. . . . Read more

Count Directories

Problem Statement Given a directory change command cd a/b/../c/d/e/../../ output the visit count of each directory such as: {a=2, b=1, c=2, d=2, e=1}. Solution

Balls and Bins

Problem Statement Given m balls and n bin, find out how many ways exist to assign the balls to then bins. Note: the buckets have no order. That is, (1,2,3) and (3,2,1) are considered the same. Example Solution This can be solved using dynamic programming. The solution builds up upon . . . Read more

Shortest Path in Directed Acyclic Graph

Problem Statement Given a weighted Directed Acyclic Graphs (DAG), find the shortest path to a particular point from the given starting point   One thing you can do is apply an O(N*N) algorithm and find the shortest distance by calculating all the distances from each node to every other node. . . . Read more