Zynga Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
It would work because at each node you are storing the sum of smaller values i.e. sum of those nodes towards that particular node's left. and while adding a new node to a tree and if you are moving towards right, ull add tat node's value and the sum value it contains, so u get the sum of previous small numbers.
This idiot was spamming his new website on chat last few days.
Now he's posting questions (which are solved in his website) pretending like they were asked in an interview today, then he's posting solutions to the questions linked to his website.
Smart idea to increase traffic to his stupid website.
How efficient would the BST solution be if the array looks like { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, etc } (increasing numbers)?
It probably needs rebalancing (AVL tree or RB tree) to avoid becoming O(N^2) in worst case.
i have written a simple program can any one help to optimize this and is there any other data structure we should use to solve this problem
#include<iostream>
using namespace std;
int main() {
// int arr[100];
int length = 5;
int sum[100];
int arr[]= {2,5,1,9,3};
for (int k = 0; k < length ; k++) {
sum[arr[k]] = 0;
}
for (int i = 0; i < length ; i++) {
for (int j = 0; j < length ; j++) {
if (arr[i] > arr[j]) {
sum[arr[i]] = sum[arr[i]] + arr[j];
}
}
}
//print the output
for(int l=0;l<length;l++){
cout<<"sum of number less then :"<<arr[l]<<"is "<<sum[arr[l]]<<endl;
}
return 0;
}
public class SumOfPrevMins
{
public static void main(String[] args)
{
int[] a = new int[]{2, 5, 1, 9, 3, 10}; //1,2,3,5,9,10
long[] b = findSumOfPrevMins(a);
for(long sum : b)
System.out.println(sum);
}
public static long[] findSumOfPrevMins(int a[])
{
if(a.length == 0)
return null;
int i = 0;
long buildNum = 0;
long b[] = new long[a.length];
while(i < a.length)
{
long tmpNum = buildNum;
buildNum = (buildNum*10) + a[i];
long sum = 0;
while(tmpNum > 0)
{
if(tmpNum % 10 < a[i])
sum = sum + (tmpNum%10);
tmpNum /= 10;
}
b[i] = sum;
i++;
}
return b;
}
}
public class SumOfPrevMins
{
public static void main(String[] args)
{
int[] a = new int[]{2, 5, 1, 9, 3, 10}; //1,2,3,5,9,10
long[] b = findSumOfPrevMins(a);
for(long sum : b)
System.out.println(sum);
}
public static long[] findSumOfPrevMins(int a[])
{
if(a.length == 0)
return null;
int i = 0;
long buildNum = 0;
long b[] = new long[a.length];
while(i < a.length)
{
long tmpNum = buildNum;
buildNum = (buildNum*10) + a[i];
long sum = 0;
while(tmpNum > 0)
{
if(tmpNum % 10 < a[i])
sum = sum + (tmpNum%10);
tmpNum /= 10;
}
b[i] = sum;
i++;
}
return b;
}
}
package FindingSumOfSmallerElementsOnLeft;
public class FindingSumOfSmallerElementsOnLeft {
public static void main(String[] args) {
int[] Arr = { 2, 3, 8, 6, 1 ,10};
//Sum of prev small numbers in arr
BST ob = new BST();
for (int i = 0; i < Arr.length; i++)
{
System.out.println(ob.AddNodeAndReturnSumOfPrevSmallNumInArr(Arr[i]));
}
ob.inOrder();
}
}
class BST
{
class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public int Sum; //for SumOfPrevSmallNumInArr
public TreeNode(int val)
{
this.data = val;
this.left = null;
this.right = null;
this.Sum = 0;
}
public TreeNode(int val, int index)
{
this.data = val;
this.left = null;
this.right = null;
this.Sum = 0;
}
public TreeNode()
{
this.data = 0;
this.left = null;
this.right = null;
this.Sum = 0;
}
}
TreeNode Root;
public BST()
{
Root = null;
}
public int AddNodeAndReturnSumOfPrevSmallNumInArr(int val)
{
TreeNode Node = new TreeNode(val);
if (Root == null)
{
Root = Node;
return 0;
}
TreeNode cur = Root;
TreeNode Prev = null;
int Sum = 0;
while (cur != null)
{
Prev = cur;
//If moving towards right add the current node's value and its
//sum value to the sum being calculated to return.
if (val >= cur.data)
{
Sum += cur.data + cur.Sum;
cur = cur.right;
}
//If moving towards left add the value to current node's sum.
else
{
cur.Sum += val;
cur = cur.left;
}
}
if (val >= Prev.data)
Prev.right = Node;
else
Prev.left = Node;
return Sum;
}
public void inOrder(){
inOrder(Root);
}
public void inOrder(TreeNode t){
if(t==null) return;
inOrder(t.left);
System.out.print(t.data + " ");
inOrder(t.right);
}
}
The code in this link makes use of BST to do it efficiently using a nice trick.
- Debugger March 27, 2014onestopinterviewprep.blogspot.com/2014/03/sum-of-previous-smaller-numbers-in-array.html
Can we improve the efficiency.further ?