Skip to content

Instantly share code, notes, and snippets.

View charlespunk's full-sized avatar

charlespunk charlespunk

View GitHub Profile
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3].
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
1.
Two binary trees are identical, if the data in each of the corresponding node are the same and the structures of them are the same.
Develop an algorithm to determine whether two binary tree are identical.
2.
Given two sorted array (ascending order), find the median of them.
For example, the median of [1, 4, 5, 7] and [2, 3, 8, 9] is 4.5
NOTE: Your algorithm MUST less than O(M + N) where M and N is the lenghths of the two sorted array. A simple merge sort is too slow for this problem. (Imagine how slow it would be if there are 2 billion data, and you need to loop over 1 billion elements.)
I saw these questions in other places and think it may be worth a try.
Question 1:
Given a finite set of unique numbers, find all the runs in the set.
Runs are 1 or more consecutive numbers.
That is, given {1,59,12,43,4,58,5,13,46,3,6},
the output should be: {1}, {3,4,5,6}, {12,13}, {43}, {46},{58,59}.
Note that the size of the set may be very large.
If you think it's too easy, you can take a look at the discussion here:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
Given height = [2,1,5,6,2,3],
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.