Achieve Internet Interview Question for Analysts


Team: artificial intelligence team
Country: United States
Interview Type: Written Test




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

My algorithm uses counting sort (or bucket sort) in O(n+m) and then count how many the elements are duplicated (count = 2). Written in C++.

Input:

3
4 4
1 2 3 4
3 4 5 6
4 5
1 2 3 4
1 2 3 5 6
3 4
1 2 3
5 6 7 4

Output:

2
3
0

Code:

#include <iostream>
#include <vector>
#include <algorithm>

bool read_array(std::vector<int>& vec, int len) {
	int n;
	
	vec.reserve(len);
	while (len--) {
		std::cin >> n;
		vec.push_back(n);
	}

	return !std::cin.fail();
}

int strength(const std::vector<int>& nvec, const std::vector<int>& mvec) {
	// Uses counting sort (or bucket sort) - O(n+m)
	std::vector<int> count_sort(101);
	for (int n : nvec)
		count_sort[n]++;
	for (int n : mvec)
		count_sort[n]++;

	// Count duplicate elements - O(100)
	return std::count(count_sort.begin(), count_sort.end(), 2);
}

int main() {
	int ntests;
	
	std::cin >> ntests;
	if (std::cin.fail())
		return 1;
		
	while (ntests--) {
		int nlen;
		int mlen;
		
		std::cin >> nlen;
		std::cin >> mlen;
		if (std::cin.fail())
			return 1;
		
		std::vector<int> nvec;
		if (!read_array(nvec, nlen))
			return 1;
		std::vector<int> mvec;
		if (!read_array(mvec, mlen))
			return 1;
			
		std::cout << strength(nvec, mvec) << std::endl;
	}
}

- Diego Giagio December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is compile error here
..
// Uses counting sort (or bucket sort) - O(n+m)
std::vector<int> count_sort(101);
for (int n : nvec)
count_sort[n]++;
for ( int n : mvec)
count_sort[n]++;

- Marey December 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Marey no, there's not. Please use a recent compiler (at least supporting C++11). You can try the code on ideone.com if you prefer.

- Diego Giagio December 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i just used ideone but still not working , ( Run time error)

- Marey December 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Marey make sure the input is correct (like the example, for instance). Otherwise you will get runtime error because the program exits with code 1.

- Diego Giagio December 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

STOP POSTING YOUR FUCKING HOMEWORK.

PEOPLE STOP ANSWERING QUESTIONS BY MAREY.

- Anonymous December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1) Declare a boolean array "A" with 101 elements
(2) Iterate through first sequence and for each number m, set A[m] to true
(3) Iterate through second sequence and for each number n, increment a counter if A[n] is true
(4) Return the counter from (3)

- Sunny December 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/python
import fileinput
import array

i = 0;
for line in fileinput.input():
    if i == 0:
        num_cases = int(line)
    elif (i-1) % 3 == 0:
        arr = array.array('i',(0,)*100)
        [len1, len2] = [int(t) for t in line.split()]
    elif (i-2) % 3 == 0:
        for t in line.split():
            arr[int(t)]+=1
    elif (i-3) % 3 == 0:
        cnt=0
        for t in line.split():
            if arr[int(t)]==1:
                cnt+=1
        print "%d" % cnt
    i+=1

- thushw December 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it's working now thxxx

- Marey December 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when i download the required file on the programming contest website , to correct it , it's like ( compile error ) while when i run it on Ideone as u said , its success and give me a right output .. so what's wrong ??

- Eliana December 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use has tables

- vikas sharan May 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
  int a[101],b[101], c[101];
  int i,j=0,k,n,m;
  printf(" enter no. of test cases");
  scanf("%d",&k);
  while(k--){
    printf("enter the sequence");
    scanf("%d %d",&n,&m);
    for(i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
    }
    for(i=0;i<m;i++)
      scanf("%d",&b[i]);

    for(i=0;i<n;i++)
    {
      c[a[i]]=1;
    }
    for(i=0;i<m;i++)
    {
      if(c[b[i]] == 1)
        j++;
    }
    printf("%d\n",j);
  }
  return 0;
}

- vikas May 09, 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