Looking for good programming challenges?

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

All posts tagged algorithms

Next permutation challenge

Problem statement Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. . . . Read more

Merge k sorted linked lists and return it as one sorted list

Problem statement Merge k sorted linked lists and return it as one sorted list. Solution We reduce the problem to the merging of two linked lists problem. This can be done in time and space. So we first write out the function to merge two sorted lists:

We can . . . Read more

Combination sum challenge

Problem statement Given a set of candidate numbers (without duplicates) and a target number , find all unique combinations in where the candidate numbers sums to . The same repeated number may be chosen from unlimited number of times. Note: All numbers (including target) will be positive integers. The solution . . . Read more

3Sum challenge

Problem statement Given an array of integers, are there elements in such that ? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array , A solution set is: Solution The idea behind the . . . Read more

Generate parentheses challenge

Problem statement Given pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given , a solution set is:

Solution

Longest palindromic substring challenge

Problem statement Given a string , find the longest palindromic substring in . You may assume that the maximum length of is . Sample input 1

Output 1

Sample input 1

Output 1

Solution We are going to iterate over each character in the input string . . . Read more

Group anagrams challenge

Problem statement Given an array of strings, group anagrams together. Sample input

Sample output

Note: All inputs will be in lower-case. Full code

Build power set of set

Problem statement Given a set of distinct integers, , return all possible subsets. Note: The solution set must not contain duplicate subsets. Sample input

Sample output

Full code

Swap list nodes in pairs challenge

Problem statement Given a linked list, swap every two adjacent nodes and return its head. For example: Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. Full code . . . Read more

Container with most water challenge

Problem statement Given n non-negative integers , where each represents a point at coordinate . vertical lines are drawn such that the two endpoints of line is at and . Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You . . . Read more