sk
BAN USER- 0of 0 votes
AnswerDescribe the SOA principles / Web Service standards follow when developing web services
- sk in United States for AWSP| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer - 0of 0 votes
AnswersWrite a method to create new tree with same structure but the values of each node will be sum of their descendents (sub tree). The leaf nodes will become 0. So if the tree is 50 30 10 40 60 55 75 (PreOrder) then new tree should be 270 50 0 0 130 0 0(PreOrder)
- sk in United States for AWSP| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Trees and Graphs
You could also use Binary search in matrix.
private bool FindXMatrix(int[][] m, int x, int row, int col)
{
if (row > m[0].Length-1 || col < 0)
return false;
if (x > m[row][col])
row++;
else if(x<m[row][col])
col--;
else
return true;
bool result = FindXMatrix(m,x, row, col);
return result;
}
That's right. We can return head of the new list. Actually I am able to write recursive function to create the sum list but assuming both are of the same length. Anyone has better idea how can we make it work for unequal length list??
private Node SumEqualListR(Node head1, Node head2)
{
int carry;
Node temp;
if (head1.next == null && head2.next == null)
{
int sum = head1.key + head2.key;
return new Node(sum);
}
temp = SumEqualListR(head1.next, head2.next);
carry = temp.key / 10;
temp.key = temp.key % 10;
Node newnode = new Node(head1.key + head2.key + carry);
newnode.next = temp;
return newnode;
}
This is what I came up with the help of Interviewer
private Node SumTree(Node root)
{
if (root == null) return null;
if (root.left == null && root.right == null)
return new Node(0);
Node left = SumTree(root.left);
Node right = SumTree(root.right);
int sum = left.key + right.key + root.left.key + root.right.key;
Node newroot = new Node(sum);
newroot.left = left;
newroot.right = right;
return newroot;
}
This is what I came up with the help of Interviewer
private Node SumTree(Node root)
{
if (root == null) return null;
if (root.left == null && root.right == null)
return new Node(0);
Node left = SumTree(root.left);
Node right = SumTree(root.right);
int sum = left.key + right.key + root.left.key + root.right.key;
Node newroot = new Node(sum);
newroot.left = left;
newroot.right = right;
return newroot;
}
A Simple and cleaner solution. No need to check for index j > i etc.
private void MaxDiff(int[] a)
{
int min = a[0]; // assume first element as minimum
int maxdiff = 0;
int posi = -1, posj = -1, minpos = 0;
for (int i = 1; i < a.Length; i++)
{
if (a[i] < min)
{
min = a[i];
minpos = i;
}
else
{
int diff = a[i] - min;
if (diff > maxdiff)
{
maxdiff = diff;
posi = minpos;
posj = i;
}
}
}
Console.WriteLine("i={0}, j={1}",posi,posj);
}
There can be different reasons why data memebers are private while we can access them through getter/setter. The main reasons to keep data members private comes from Data Abstraction principle.
- The abstract properties(so called getters/setters here) are those that are visible to client code that makes use of the data members (the interface to the data members) while the concerete implmenetation is kept private and indeed can change.
-- The idea is that such changes are not supposed to have any impact on client code ( called Localization of Change). Code that uses the object in question will not need to be edited if the implementation of the object is chnaged. since any changes to the implmentation must still comply with the interface and since client code using object may only refer to properties and abilities specified in the interface as changes can be made to the implmentation without requiring any changes in code where the object is used.
-- The uses doesn't need to know any technical knowledge of how the implementation works to use an object. The implementation can be complex but will be encapsultaed (Encapsulation) in a simple interface. For e.g. if we talk about a Banking system, there can be Account class which may have complex implementation, how the account balance is changed. User of this class should not be allowed to access the balance (data member) directly and modify; but using the properties (interface to the data members), you may enforce certain rules how balance can be modified)
keeping data members private also makes your object secure from malicious software which may instantiate the object and perform directly some manipulation on data members. If it goes through properties (getters/setters) you may have some control.
Using above Account class example, the malicous software may not be able to set the account balance to large arbitrary value as with getters and setters you may have some validation plus you may never expose setters for such important data members.
They can only be accessed by the operations which will have all those validations/rules which disallows to set a Account balance to a high arbitrary value. Going through properties/operations will also make a log of the activities.
At high level my thoughts are
- IE seems to consume lot of memory just for few tabs opened. Either there are memory leaks or it allocates too many objects. We have to profile IE and improve in this area
- IE seems to create process for each tab opened. That also seems to be overhead
- The algorithms/design needs to be revisited used in IE. For e.g. It may doing something too much with respect to security / plug ins etc.
int[] f = new int[100];
internal void fibo(int start, int length)
{
f[0] = 0; // initialize first position to zero
for (int i = 0; i < length; i++)
{
int num = fibo(start + i);
Console.WriteLine(num);
}
}
private int fibo(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
f[n - 1] = fibo(n - 1);
return f[n - 1] + f[n - 2];
}
- sk January 18, 2012Yes. In general this can be acheived at O(n) time. Here is my c# version
internal void BestBuySellingTime()
{
int[] a = { -2, 4, 30, 140, -50, 90, -60, 100, 120 };
// 9AM 10AM 11AM 11:30 12PM 12:30 1 PM 2PM 3PM 4 PM
// Get the max.. profit
// best buying price = -60
// best selling price = 120
int min = a[0];
int maxProfit = 0;
for (int i = 1; i < a.Length; i++)
{
if (a[i] < min)
{
min = a[i];
}
else
{
if (a[i] - min > maxProfit)
{
maxProfit = a[i] - min;
}
}
}
Console.WriteLine("Max Profit " + maxProfit);
}
- sk December 05, 2011Here is a recrusive solution in c#
internal void NumberAsc()
{
NumberAsc(3, new StringBuilder(), 0);
}
private void NumberAsc(int n, StringBuilder sb, int lastDigit)
{
if (n == 0)
{
Console.WriteLine(sb.ToString());
return;
}
for (int i = 1; i <= 9; i++)
{
if (i > lastDigit)
{
sb.Append(i);
NumberAsc(n - 1, sb, i);
sb.Length = sb.Length - 1;
}
}
}
RepNoraMiller, abc at A9
Confident and dedicated photographer with experience in both professional and freelance photography.. Prioritizes communication on the job to avoid errors ...
Sparse Matrix
- sk March 09, 2012