Yelp Interview Question for Software Engineers


Country: United States




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

Keep a hashtable (Call it h)
Keep a sum which represents the dot product value (Call it s)

Now, the idea is that we will add to h whatever mapping we encounter when reading the lines. If there is no such mapping for the vector position, we will add the mapping. if there is, then we simply multiply the value by the mapped value and then add it to the sum (s)

Return s.

For example, we first add the key value pair <1, 4>
When we encounter <1, 7>, we see that there already exists a mapping for the key <1>
So we look at the value to that key (4) and multply it by our current value (7) and add to the sum. Same thing happens when <5, 3> is originally added - nothing happens, but when we see that we can add <5, 1>, we multply 3 and 1 and add to the sum.

- Skor March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was this code asked during the hackerrank test?

- Anonuy March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package vector;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class DotProduct {
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String noElements = in.nextLine();
		String[] kn = noElements.split("\\s+");
		int k = Integer.parseInt(kn[0]);
		int n = Integer.parseInt(kn[1]);
		Map<Integer, Integer> vector1 = new HashMap<Integer, Integer>();
		Map<Integer, Integer> vector2 = new HashMap<Integer, Integer>();
		int size = 0;
		for(int i=0; i<k; i++) {
			String line = in.nextLine();
			String[] split = line.split("\\s+");  
			int key = Integer.parseInt(split[0]);
			int value = Integer.parseInt(split[1]);
			vector1.put(key, value);
			size = size>key ? size : key;
		}
		for(int i=0; i<n; i++) {
			String line = in.nextLine();
			String[] split = line.split("\\s+");  
			int key = Integer.parseInt(split[0]);
			int value = Integer.parseInt(split[1]);
			vector2.put(key, value);
			size = size>key ? size : key;
		}
		int dotProd = 0;
		for(int i=0; i<=size; i++) {
			if(vector1.get(i) != null && vector2.get(i) != null) {
				dotProd += (vector1.get(i) * vector2.get(i));
			}
		}
		System.out.println(dotProd);
	}
}

- Neo March 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To improve on comment above's code, we can do this with just a single hashmap. Just check if the value exists in the map in the second loop over n's values and if it does, increment the result to be:

if(vector.get(key) != null)
        dotProd += (vector.get(key) * value)

- Lucas Crawford April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the more efficient solution:

public class SparseVectorDotProduct {

    public static void main(String args[]){

        Scanner in = new Scanner(System.in);
        String noElements = in.nextLine();
        String[] kn = noElements.split("\\s+");
        int firstVector = Integer.parseInt(kn[0]);
        int secondVector = Integer.parseInt(kn[1]);

        Map<Integer, Integer> map = new HashMap<Integer,Integer>();
        
        int result = 0;

        for(int i=0; i<firstVector; i++) {
            String line = in.nextLine();
            String[] split = line.split("\\s+");
            int key = Integer.parseInt(split[0]);
            int value = Integer.parseInt(split[1]);
            map.put(key, value);
        }

        for(int i=0; i<secondVector; i++) {
            String line = in.nextLine();
            String[] split = line.split("\\s+");
            int key = Integer.parseInt(split[0]);
            int value = Integer.parseInt(split[1]);
            if(map.containsKey(key) && map.get(key)!=null) {
                int tempvalue = map.get(key) * value;
                result += tempvalue;
            }
        }

        System.out.print(result);
    }

}

- Jvalant April 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SparseVectorDotProduct {

    public static void main(String args[]){

        Scanner in = new Scanner(System.in);
        String noElements = in.nextLine();
        String[] kn = noElements.split("\\s+");
        int firstVector = Integer.parseInt(kn[0]);
        int secondVector = Integer.parseInt(kn[1]);

        Map<Integer, Integer> map = new HashMap<Integer,Integer>();
        
        int result = 0;

        for(int i=0; i<firstVector; i++) {
            String line = in.nextLine();
            String[] split = line.split("\\s+");
            int key = Integer.parseInt(split[0]);
            int value = Integer.parseInt(split[1]);
            map.put(key, value);
        }

        for(int i=0; i<secondVector; i++) {
            String line = in.nextLine();
            String[] split = line.split("\\s+");
            int key = Integer.parseInt(split[0]);
            int value = Integer.parseInt(split[1]);
            if(map.containsKey(key) && map.get(key)!=null) {
                int tempvalue = map.get(key) * value;
                result += tempvalue;
            }
        }

        System.out.print(result);
    }

}

- Jvalant April 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
import java.lang.*;
import java.io.*;

import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
	public static void main (String[] args)
	{
		// your code goes here
		Scanner in = new Scanner(System.in);
		int m = in.nextInt();
		int n = in.nextInt();
		int [][] arrm = new int[m][2];
		int [][] arrn = new int[n][2];
		for(int i=0;i<m;i++){
			arrm[i][0] = in.nextInt();
			arrm[i][1] = in.nextInt();
		}
		for(int i=0; i<n;i++){
			arrn[i][0] = in.nextInt();
			arrn[i][1] = in.nextInt();
		}
		int result=0;
      	       for(int i=0;i<arrm.length;i++){
			if(arrm[i][0]==arrn[i][0]){
			 result = result + (arrm[i][1]* arrn[i][1]);
			}
		}
		System.out.println(result);
	}
}

- RD September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
import java.lang.*;
import java.io.*;

import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
	public static void main (String[] args)
	{
		// your code goes here
		Scanner in = new Scanner(System.in);
		int m = in.nextInt();
		int n = in.nextInt();
		int [][] arrm = new int[m][2];
		int [][] arrn = new int[n][2];
		for(int i=0;i<m;i++){
			arrm[i][0] = in.nextInt();
			arrm[i][1] = in.nextInt();
		}
		for(int i=0; i<n;i++){
			arrn[i][0] = in.nextInt();
			arrn[i][1] = in.nextInt();
		}
		int result=0;
      	       for(int i=0;i<arrm.length;i++){
			if(arrm[i][0]==arrn[i][0]){
			 result = result + (arrm[i][1]* arrn[i][1]);
			}
		}
		System.out.println(result);
	}
}

- RD September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution
{
	public static void main (String[] args)
	{
		Scanner in = new Scanner(System.in);
		int m = in.nextInt();
		int n = in.nextInt();
		int [][] arrm = new int[m][2];
		int [][] arrn = new int[n][2];
		for(int i=0;i<m;i++){
			arrm[i][0] = in.nextInt();
			arrm[i][1] = in.nextInt();
		}
		for(int i=0; i<n;i++){
			arrn[i][0] = in.nextInt();
			arrn[i][1] = in.nextInt();
		}
		int result=0;
          	for(int i=0;i<arrm.length;i++){
			if(arrm[i][0]==arrn[i][0]){
			 result = result + (arrm[i][1]* arrn[i][1]);
			}
			
		}
		System.out.println(result);
	}
}

- RD September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

k,x = input().split()
sum = 0
res = {}
for i in range(int(k)):
    key,val = input().split()
    res[key] = int(val)
    
for i in range(int(x)):
    key,val = input().split()
    if key in res:
        res[key]*=int(val)
        sum+=res[key]
print(sum)

- Sindhu October 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

k,x = input().split()
sum = 0
res = {}
for i in range(int(k)):
key,val = input().split()
res[key] = int(val)

for i in range(int(x)):
key,val = input().split()
if key in res:
res[key]*=int(val)
sum+=res[key]
print(sum)

- Sindhu October 23, 2018 | 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