vj
BAN USERAt a high level there are 3 things one or more of which may be slow:
01. Your computer, 02. internet connection, 03. the server
01. Your computer it can be slow because of some anti virus scan, some process using high processor/network, HDD may be full, your computer may be virus infected, and thousands other possible reasons.
02. Internet: Your proxy setting may be using a slow proxy, your ISP may be slow, the server may have failed over to a different Geo so it is taking long time to route the calls, etc.
03. Slow server: Server might be under DOS attack, All the instances may not be up, because of festival season/deal/new offering much more than usual # of people are using the site, servers running out of space, Networking issue in data center, slow or dead partner servers, etc. to name a few.
I would ask more questions to the interviewer, if it is dumb load balancer a round robin will work like Aresh mentioned, otherwise we'll have to have a policy by which we will decide the request goes to which server. like random, scatter and gather, sticky session, based on some parameter.
Look at this page for some ideas: http :// horicky.blogspot.com/2010/10/scalable-system-design-patterns.html
private static int GetFibonacciSum(out int first, out int second, int count){
if (count < 1){
throw new ArgumentException("count has to be > 0");
}
if (count == 1){
first = 0; second = 0;
return 0;
}
if (count == 2){
first = 0; second = 1;
return 1;
}
int firstSub; int secondSub;
int sum = GetFibonacciSum(out firstSub, out secondSub, count - 1);
second = firstSub + secondSub;
first = secondSub;
return sum + second;
}
01. It'll give compile time error given main "}" is not present.
02. If you add "}" at the end then:
(a) it'll give warning for "i" is declared but not used.
(b)for(test();test();test()) {/* do somethibng */}
translates into
for(3;3;3)
i.e. for(initial condition; FALSE; LoopExpression)
it'll print 2 once, as in C++ 0 is TRUE and >0 is FALSE.
Stack will make the code more readable and simple as well:
public static void PrintKnodeTillTail(LinkedList list, int k){
if (k < 1 || list == null)
return;
LinkedList regularPtr = list;
LinkedList kAheadPtr = list;
// Move the first pointer K nodes ahead
for (int i = 0; i < k; i++){
kAheadPtr = kAheadPtr.Next;
if (kAheadPtr == null)
return;
}
// when kAhead reaches end regularptr reaches K node before end
while (kAheadPtr != null){
kAheadPtr = kAheadPtr.Next;
regularPtr = regularPtr.Next;
}
// push the K nodes to stack
Stack<LinkedList> stk = new Stack<LinkedList>();
while (regularPtr != null){
stk.Push(regularPtr);
regularPtr = regularPtr.Next;
}
// pop and print them
while (stk.Count > 0)
Console.WriteLine(stk.Pop().Data);
}
Non Recursive:
public static Node ReturnKthItemNonRecursive(Node root, int k){
if (k <= 0 || root == null)
return null;
int i = 0;
Node node = root;
Stack<Node> stk = new Stack<Node>();
while (node != null){
do{
stk.Push(node);
node = node.Right;
} while (node != null);
node = stk.Pop();i++;
do{
if (i == k)
return node;
if (node.Left == null){
if (stk.Count == 0)
return null;
node = stk.Pop();i++;
}
} while (node.Left != null);
node = node.Left;
}
return null;
}
Recursive code:
public static Node ReturnKthItem(Node root, int k){
int currPosition = 0;
Node kthNode = null;
ReturnKthItem(root, k, ref currPosition, ref kthNode);
return kthNode;
}
private static bool ReturnKthItem(Node root, int k, ref int currPosition, ref Node kthNode){
if (root == null)
return false;
if (!ReturnKthItem(root.Right, k, ref currPosition, ref kthNode)){
currPosition = currPosition + 1;
if (k == currPosition){
kthNode = root;
return true;
}
else if (ReturnKthItem(root.Left, k, ref currPosition, ref kthNode)){
return true;
}
else{
return false;
}
}
else{
return true;
}
}
here you go:
public void TraverseNode(Node node)
{
Queue<Node> queue = new Queue<Node>();
if (node == null)
return;
int nodesInCurrentLevel = 1; int nodesInNextLevel = 0;
queue.Enqueue(node);
while (queue.Count > 0)
{
Node nodeToTraverse = queue.Dequeue();
if (nodeToTraverse.Left != null)
{
queue.Enqueue(nodeToTraverse.Left); nodesInNextLevel++;
}
if (nodeToTraverse.Right != null)
{
queue.Enqueue(nodeToTraverse.Right); nodesInNextLevel++;
}
Console.Write(nodeToTraverse.Data);
nodesInCurrentLevel--;
if(nodesInCurrentLevel == 0)
{
Console.WriteLine();
nodesInCurrentLevel = nodesInNextLevel;
nodesInNextLevel = 0;
}
}
}
And since you have to print each node, you have to traverse each node and you always do that so the complexity will be O(n).
- vj April 02, 2013
01. In most of the languages bool data is stored as a byte so you are still wasting 7 bits out of 8.
- vj June 09, 201302. this code will not identify duplicates because bool will not tell you if a number has only appeared once or multiple times.
03. Of course this code will NOT return the "first" non repeated character.