Interview Question for fresherss


Country: India
Interview Type: Written Test




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

#include<bits/stdc++.h>
using namespace std;
int prime(int n)
{
int flag=0,i;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
flag=1;
}
if(flag==0)
return 0;
}
int main()
{
int a,i=0,j,k,n,b;
cin>>a;
cin>>n;
while(i<=n)
{
b=++a;
j=prime(b);
if(j==0)
i++;
if(i==5){
cout<<b;
break;
}
a=b;

}
}

- Anonymous September 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++, Caching

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

vector<int> primes;

void initPrime(int a) {
	int i, j, l;
	bool p;

	for (i = 2; i <= a; i++) {
		l = (int)sqrt((double)i);
		p = true;
		for (j = 0; j < primes.size(); j++) {
			if (l < primes[j]) break;
			if (i % primes[j] == 0) {
				p = false;
				break;
			}
		}
		if (p) primes.push_back(i);
	}
}

bool isPrime(int n) {
	int i, l;

	if (n < 2) return false;

	l = (int)sqrt((double)n);
	for (i = 0; i < primes.size(); i++) {
		if (l < primes[i]) break;
		if (n % primes[i] == 0) return false;
	}
	primes.push_back(n);

	return true;
}

int solve(int a, int n) {
	initPrime(a);

	while (n) {
		if (isPrime(++a)) n--;
	}

	return a;
}

int main(int argc, char* argv[]) {
	int a, n;

	cin >> a >> n;
	cout << solve(a, n) << "\n";

	return 0;
}

- kyduke September 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java code:

public class PrimeNumber {
	
	public void print(int a, int n) {
		while (n != 0) {
			if (isPrime(++a)) {
				--n;
			}
		}
		System.out.println(a);
	}
	
	private boolean isPrime(int a) {
		if (a == 2) {
			return true;
		} else if(a < 2 || a % 2 == 0) {
			return false;
		}
		for (int i = 3; i*i <= a; i += 2) {
			if (a % i == 0) {
				return false;
			}
		}
		return true;
	}
	
}

Quadratic time, constant space complexity. If we have maximum limit for output prime number, we could use Sieve of Eratosthenes to calculate all prime numbers beforehand. Thus, complexity would be O(n log log n) time and O(N) space.

- Iuri Sitinschi September 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A prime number could be in format of 6n+-1 so check for numbers in this format if they are prime or not upto the given count.

int c = 0;
	int n = 1;
	int lastPrime=-1;
	while( c < N ) {
		int k1 = 6*n-1;
		int k2 = 6*n +1;
		if( isPrime(k1) )	{ lastPrime = k1; c++; }
		if(c == N)	break;
		if(isPrime(k2) )		{ lastPrime = k2; c++; }
		n++;
	}
	return lastPrime;

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

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
//var_dump($n);
for($i=1;;$i++)
{ $n=$n+1;
// var_dump($n);
if($n%2!=0){
$p[]=$n;
if(count($p)==5)
break;
}
}
var_dump(end($p));

- Anonymous October 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
//var_dump($n);
for($i=1;;$i++)
    {  $n=$n+1;
    // var_dump($n);
      if($n%2!=0){
          $p[]=$n;
         if(count($p)==5)
             break;
          }    
}
var_dump(end($p));

- Anonymous October 09, 2016 | 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