## 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;
return 1;
std::vector<int> mvec;
return 1;

std::cout << strength(nvec, mvec) << std::endl;
}
}``````

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

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]++;

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

@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.

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

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

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

@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.

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

PEOPLE STOP ANSWERING QUESTIONS BY MAREY.

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)

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``````

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

it's working now thxxx

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 ??

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

use has tables

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

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.

### 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.