First Problem ఠ_ఠ
Convert a given Binary Tree to Doubly Linked List
Solution
Inorder traversal, then link each value…
|
|
The Great Tree-List Recursion Problem
Here’s the formal problem statement: Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of the tree nodes. The “previous” pointers should be stored in the “small” field and the “next” pointers should be stored in the “large” field. The list should be arranged so that the nodes are in increasing order. Return the head pointer to the new list. The operation can be done in O(n) time — essentially operating on each node once. Basically take figure-1 as input and rearrange the pointers to make figure-2.
Solution
|
|
LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
LinkedHashMap Solution
|
|
Map + List Solution
|
|
Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
Binary Search Solution
- Aside form the obvious scan solution..
|
|
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
Solution
|
|
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Fast Slow Pointer Solution
- Edge case one: input <=1 element
- Edge case two: fast==null || fast.next==null
|
|
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Follow up:
Can you solve it without using extra space?
Solution
- fast dis = a+b+c+b = 2 X slow dis = 2 * (a+b) -> a=c
|
|