Zynga Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 4 vote

The code in this link makes use of BST to do it efficiently using a nice trick.

onestopinterviewprep.blogspot.com/2014/03/sum-of-previous-smaller-numbers-in-array.html

Can we improve the efficiency.further ?

- Debugger March 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The algo in the link is not clear. Can you elaborate why will it work?

- kr.neerav March 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

- codechamp March 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous March 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Guy March 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@ananymous if its true then i must say smart move, but not smart enough to not get caught :) But whatever might be the reason we got to learn a new question

- kr.neerav March 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

F***ER is trying to advertize his blog.

- Anonymous March 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

codechamp, are you sure Zynga asked someone this exact question?

- S O U N D W A V E March 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- gyaan April 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
	}		
}

- Jagadish July 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}
}

- Jagadish July 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
        }
}

- codeAddict August 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In python
=============

import json
x_inp=input("Enter the array:")
x=json.loads(x_inp)
y=int(input("Enter the index value:"))
def mini(x,y):
    val=x[y]
    summed=0
    for i in range(0,(y+1)):
        if x[i]<val:
            summed+=x[i]
    return summed
mini(x,y)

- jagannathans92 March 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Using Priority Queue will work.

Maintain a min heap priority queue (i.e. smallest element is on the top)

For each element in Array 
{
   For current number N, 
   Pop elements from Heap till root is smaller than N and maintain sum
   Push all poped elements back and push current
}

- Mithya March 28, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More