Tuesday, March 5, 2013

Rotate List

Rotate ListMar 28 '12
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
Analysis An alternative method to handle the case where k>n is to not wrap around the faster ptr but to traverse the list once to get number of elements in the list and then adjust k=k%n or n

Remove Duplicates from Sorted List


Remove Duplicates from Sorted ListApr 22 '12
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.
Analysis
This is a O(n) time complexity algorithm that links the last distinct element with the current distinct element, thereby deleting all elements between these two nodes.

Merge k Sorted Lists


Merge k Sorted ListsFeb 14 '12
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Monday, March 4, 2013

Merge Two Sorted Lists

Merge Two Sorted ListsMar 30 '12
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
This is an algorithm with O(n) time complexity. List L2 is merged in list L1 and returned as a new list.

Merge two sorted arrays


Merge Sorted ArrayMay 20 '12
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

Sunday, March 3, 2013

String to Integer (atoi)


String to Integer (atoi)Dec 27 '11
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Insert Interval

Insert IntervalMar 27 '12
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:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

Algorithm idea
While traversing the input intervals, merge with all intervals conflicting with the new interval to be inserted. If there are no conflicting intervals, then find the correct position to insert this new interval based on the start value of the new interval relative to the intervals in the input interval vector.

Time Complexity: O(n)
Space Complexity: O(1)