Mi Jalgaonkar
BAN USER- 0of 0 votes
AnswersThere is a very Primitive Database and it has a table say "Travel". The content of table is as follows:
- Mi Jalgaonkar in United States
Source | Dest
--------------
Sea | LA
LA | FL
LA | MA
FL | Sea
Sea | FL
The ask is to find out all routes between (Sea) to (FL) with mininum hop.
the Result would be:
1. Sea -> FL
2. Sea -> LA - > FL
You have to write a Middle tier function to achieve above result. You can assume there is DBAPI that return the Destination city if you provide the source city.| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test
public void FindSiblings(TreeNode root)
{
Queue<TreeNode> q = new Queue<TreeNode>();
Queue<int> lq = new Queue<int>();
q.Enqueue(root);
lq.Enqueue(0);
TreeNode previous = null;
TreeNode current = null;
int level = -1;
int previousLevel = -1;
while (q.Count > 0)
{
current = q.Dequeue();
level = lq.Dequeue();
if (current.Left != null)
{
q.Enqueue(current.Left);
lq.Enqueue(level + 1);
}
if (current.Right != null)
{
q.Enqueue(current.Right);
lq.Enqueue(level + 1);
}
if (previous != null && previousLevel == level)
{
previous.Sibling = current;
Console.WriteLine(previous.Data + " : " + previous.Sibling.Data);
}
previous = current;
previousLevel = level;
}
}
class TreeNode
{
public int Data { get; set; }
public TreeNode Left { get; set; }
public TreeNode Right { get; set; }
public TreeNode GrandParent { get; set; }
public TreeNode(int data)
{
this.Data = data;
this.Left = null;
this.Right = null;
this.GrandParent = null;
}
public void SetGrandParent(TreeNode root)
{
Queue<TreeNode> q = new Queue<TreeNode>();
q.Enqueue(root);
TreeNode current = null;
while (q.Count > 0)
{
current = q.Dequeue();
if (current.Left != null)
{
q.Enqueue(current.Left);
CheckChildren(current.Left, current);
}
if (current.Right != null)
{
q.Enqueue(current.Right);
CheckChildren(current.Right, current);
}
}
}
private void CheckChildren(TreeNode parent, TreeNode grandParent)
{
if (parent.Left != null)
{
parent.Left.GrandParent = grandParent;
}
if (parent.Right != null)
{
parent.Right.GrandParent = grandParent;
}
}
public void DisplayFamily(TreeNode node)
{
if (node == null)
{
return;
}
DispayFamily(node.Left);
Console.WriteLine(" Grand father of {0} is : {1}", node.Data, (node.GrandParent == null) ? "Null" : node.GrandParent.Data.ToString());
DispayFamily(node.Right);
}
}
public Node ReverseList(Node Head)
{
Node loop = Head;
Node reverse = null;
Node temp = null;
while (loop != null)
{
temp = loop.Next;
loop.Next = reverse;
reverse = loop;
loop = temp;
}
return reverse;
}
public void PrintSpiralMatrix()
{
int j = 0;
for (int i = m_size - 1; i >= 0; i--, j++)
{
for (int k = j; k < i; k++)
Console.WriteLine(matrix[j][k]);
for (int k = j; k < i; k++)
Console.WriteLine(matrix[k][i]);
for (int k = i; k > j; k--)
Console.WriteLine(matrix[i][k]);
for (int k = i; k > j; k--)
Console.WriteLine(matrix[k][j]);
}
//special case for middle element if N is odd
if (m_size % 2 == 1)
{
Console.WriteLine(matrix[(m_size - 1) / 2][(m_size - 1) / 2] );
}
}
public class Cell
{
public int Row { get; set; }
public int Col { get; set; }
public Cell(int i, int j)
{
Row = i;
Col = j;
}
}
public class MatrixProblem
{
private static void Display2DArray(int[][] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine();
Console.Write("\t [ ");
for (int j = 0; j < arr[0].Length; j++)
{
Console.Write(string.Format("{0}{1} ", arr[i][j], (j < arr[0].Length - 1) ? "," : ""));
}
Console.Write("]");
}
Console.WriteLine();
}
public static List<Cell> Scan2DArrayForZero(int[][] arr)
{
List<Cell> cells = new List<Cell>();
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[0].Length; j++)
{
if (arr[i][j] == 0)
{
Cell newCell = new Cell(i, j);
cells.Add(newCell);
}
}
}
return cells;
}
public static void ConvertRowColOf2DArrayForZero(int[][] arr, List<Cell> cells)
{
foreach (Cell cell in cells)
{
for (int i = 0; i < arr[0].Length; i++)
{
arr[cell.Row][i] = 0;
}
for (int j = 0; j < arr.Length; j++)
{
arr[j][cell.Col] = 0;
}
}
}
public static void Main()
{
int[][] arr = new int[][]
{
new int[] { 1, 1, 1, 1, 1 },
new int[] { 1, 0, 1, 1, 1 },
new int[] { 1, 1, 1, 1, 1 },
new int[] { 1, 1, 1, 0, 1 },
new int[] { 1, 1, 1, 1, 1 }
};
Console.WriteLine("\n\n\t Original Matrix:");
Display2DArray(arr);
List<Cell> cells = Scan2DArrayForZero(arr);
ConvertRowColOf2DArrayForZero(arr, cells);
Console.WriteLine("\n\n\t Resultant Matrix:");
Display2DArray(arr);
Console.WriteLine();
}
}
Output:
=======
Original Matrix:
[ 1, 1, 1, 1, 1 ]
[ 1, 0, 1, 1, 1 ]
[ 1, 1, 1, 1, 1 ]
[ 1, 1, 1, 0, 1 ]
[ 1, 1, 1, 1, 1 ]
Resultant Matrix:
[ 1, 0, 1, 0, 1 ]
[ 0, 0, 0, 0, 0 ]
[ 1, 0, 1, 0, 1 ]
[ 0, 0, 0, 0, 0 ]
[ 1, 0, 1, 0, 1 ]
static void Main(string[] args)
{
int [] array = {0,0,2,0,3,0,0,4,5,6,7,0,0,8};
int hole = 0;
for (int j = 0; j < array.Length; j++)
{
if (array[j] != 0)
{
array[hole++] = array[j];
array[j] = 0;
}
}
Console.WriteLine("\n After Moving zeroes to end...");
for (int i = 0; i < hole; i++)
Console.Write(array[i].ToString() + ", ");
}
- Mi Jalgaonkar November 03, 2010
- Mi Jalgaonkar October 29, 2014