Monday, June 20, 2016

#codingexercise
Given an array of n numbers with repetition of numbers. You need to find the max length of continuous sub array with at max 3 unique elements.

int maxSubArrayLen(List<int> nums)
{
int len = 0;
int len_window = 0;
for (int i =0; i < nums.Count; i++)
{
len_window = len_window + 1;
if (nums.GetRange(i-len_window, len_window).distinct() > 3 )
     len_window = 0;
else if (len < len_window)
     len = len_window;
}
return len;
}

Take two trees. Invert the second and add it to the bottom of the first tree. Do they have nodes that overlap ?
void GetLeafNodes(node root, ref List<node> result)
{
if (root == null) return;
if (root.left == null && root.right == null) result.add(root);
GetLeafNodes(root.left, ref result);
GetLeafNodes(root.right, ref result);
}

void IsOverlap(node tree1, node tree2)
{
var leaves1 = new List<node>();
var leaves2 = new List<node>();
GetLeafNodes(tree1, ref leaves1);
GetLeafNodes(tree2, ref leaves2);
if (leaves1.Intersect(leaves2)) return true;
else return false;
}

Find a missing card in a standard deck of playing cards where you can remove one card at at time from the deck.
K = number of suits ( say 4 eg 1, 2, 3, 4 for leaf, clover, spade, diamond)
N = number of denominations in each suit (say 13)

card GetMissingCard(List<card> deck, int K, int N)
{
bool found = new bool[K * N]();
for (int i = 0;i < K * N  - 1; i++)
{
found[(deck[i].K-1)*N + deck[i].N-1] = true;
}
var missing = found.toList().First( x => x == false);
return missing;
}

No comments:

Post a Comment