Epic Systems Interview Question for Software Engineer / Developers






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

//Java class
class TestPrimes{
public static void printPrimes(int n){
if ( n <= 1)
return;
in all[] = new int[n+1];
for (int i = 0; i <= n; i++){
all[i] = i;
}

int prime[] = new int[n+1];
for (int i = 0; i <= n; i++)
prime[i] = 1;

int x = 2;
while (x <= n){
if (prime[x] == 1){
System.out.println(x);
for (int j = 1; j * x <=n; j++)
prime[j*x] = 0;
}
x++;
}
}
}

- css March 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int state,result;
int num = 97;
int i;
for ( i = 2; i<num; i++){
result = num%i;
if (result ==0)
state = 0;
}
if (state !=0)
printf("this is prime ");
if (state ==0)
printf("this is not prime ");

- adhikarimanoj1 June 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sieve. wikipedia should have it.

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

#include <stdio.h>
main()
{
int n,i=1,j,c;
clrscr();
printf("Enter Number Of Terms
");
scanf("%d",&n);
printf("Prime Numbers Are Follwing
");
while(i<=n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
if(c==2)
printf("%d",i)
i++;
}
getch();
}

- Pranany Kuruvilla March 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void findPrime(int n)
	{
		for(int i = 2 ; i < n ; i ++)
		{
			flag = false ; 
			
			for(int j = 2 ; j < Math.sqrt(i * 1.0) ; j++)
			{
				if(i%j==0)
				{	
				   flag = true ; 
					break ; 
				}
			}	
			if(flag ==false)
			   {
			System.out.println(i +" is a Prime Number");
			   }

		   }	

	}

- Rushabh Shah March 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this method is incorrect. it says 4 is a prime number

- tj May 27, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. because the loop condition for j should be "j <= Math.sqrt(i * 1.0)"

- Girish June 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void findPrime(int num)
{
   for (int i=1; i<=num; i++)
   {
       if(isPrime(i))
    	  System.out.println(i +" is a Prime Number");
   }
}

public boolean isPrime(int num)
{
    int divisor = Math.floor(Math.sqrt(num)); //Our largest divisor
    if (num == 2)
       return true;
    else if (num < 2)
       return false;
    for (i = 2; i <= divisor; i++) 
    {
        if (num % i == 0)
           return false;
    }
    return true;
}

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

don't need to consider the even numbers in the above calculation - thus reducing complexity --check out "Sieve of Eratosthenes" method from wikipedia...

- pondy April 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printPrime(int N)
{
int * numbers = malloc( N * sizeof(int));
for(int i = 0; i < N; i++)
{
*(numbers + i) = i+1;
}
for(int i = 1; i < N; i++)
{
if(*(numbers + i) != 0)
{
for(int j = i+1; i < N; j++)
{
if(*(numbers + j) % *(numbers + i) == 0)
{
*(numbers + j) = 0;
}
}
}
}
for(int i = 1; i < N; i++)
{
printf("%d is a prime\n", *(numbers + i));
}
free(numbers);
}

- c.c. November 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

getprime(int N)
{	int[] a;
	a.append(2);
	for(i=3;i<=N;i=i+2)	//assuming N inclusive and considering only odd numbers
	{	for(j=0;j<a.length;j++)
		{	if(i%a[j]==0)
				break;
		}
		if(i%a[j]==0)
			continue;
		else
			a.append[i];
	}
	return a;
}

O(np/2) == O(np)
p is number of prime from o to n

- cool February 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2 is the only even prime number. so u can check only for odd numbers to be prime between 2 and N

- Riva November 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey38306" class="run-this">public void PrimeNumbers (int N) {

boolean not_prime = false;

for (int i=2; i<=N; i++) {
int j = 2;
not_prime = false;
while (j <= sqrt(i)) {
if (i%j == 0) {
not_prime = true;
break;
} // end if
j++;
} // end while

if (!not_prime) {
System.out.println("Prime number:" + i);
} // end if
} // end for
}

</pre><pre title="CodeMonkey38306" input="yes">
19</pre>

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

Can someone tell me what programming language is being used to answer this question?

- Kdoshi August 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{ int prime,i,j,n,t=0;
printf("Enter value of n upto which prime no is to find\n");
scanf("%d",&n);
printf(" The prime no are \n");
for(i=2;i<=n;i++)
{ t=0;
for(j=2; j<i; j++)
{ if(i%j==0)
t++;
}
if(t==0)
printf("%d, ",i);
}
}

- @123 August 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class getprime {
	public static void main(String[] args) {

        for(int i=0; i<50; i++) {
            if(isPrime(i)) { 
            	 System.out.println(i);
            }
        }
    }
	private static boolean isPrime(int n) {
	    boolean result = false;
	    if(n == 2 || n == 3) return true;

	    for (int i = 2; i <= (int) Math.sqrt(n); i++) {
	        if (n % i == 0) {
	            result = false;
	            break;
	        } else
	            result = true;
	    }
	    return result;
	}

}

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

public class Prime {

	public static void main(String args[]) {
		int p = 329;

		boolean check = false;

		for (int i = 2; i <= p; i++) {
			if (i == 2 || i == 3)
				System.out.println(i);
			for (int j = 2; j <= i / 2; j++) {

				if (i % j == 0) {
					check = false;
					break;
				} else
					check = true;
			}
			if (check)
				System.out.println(i);
		}

	}
}

- sanjeev kaneria March 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void prime(int max) {
        int count;
        for (int i = 2; i <= max; i++) {
            count = 0;
            for (int j = 2; j <= (int)Math.sqrt(i); j++) {
                if (i % j == 0) {
                    count++;
                }
            }
            if (count == 0) {
                System.out.println(i);
            }
        }
    }

- junpos June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is a working code in C++. I have applied Sieve of Eratosthenes:

#include <cstdio>
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int n, i, j, **arr, k, x, y;
    cin>>n;
    arr=new int *[(int)ceil(sqrt(n))];
    for (i=0; i<(int)ceil(sqrt(n)); i++)
    {
        arr[i]=new int[(int)ceil(sqrt(n))];
    }
    for (i=0; i<(int)ceil(sqrt(n)); i++)
    {
        for (j=0; j<(int)ceil(sqrt(n)); j++)
        {
            arr[i][j]=(i*((int)ceil((sqrt(n)))))+(j+1);
        }
    }
    for (i=0; i<(int)ceil(sqrt(n)); i++)
    {
        for (j=0; j<(int)ceil(sqrt(n)); j++)
        {
            if (arr[i][j]==-1 || arr[i][j]==1)
            {
                continue;
            }
            else
            {
                for (k=(2*arr[i][j]); k<=n; k++) 
                {
                    x=k/((int)ceil((sqrt(n))));
                    y=k%(((int)ceil((sqrt(n)))));
                    if(y==0)
                    {
                        x--;
                        y=((int)ceil((sqrt(n))));
                    }
                    y--;
                    if (k%arr[i][j]==0)
                    {
                        arr[x][y]=-1;
                    }
                }
            }
        }
    }
    for (i=0; i<(int)ceil(sqrt(n)); i++)
    {
        for (j=0; j<(int)ceil(sqrt(n)); j++)
        {
            if (arr[i][j]==1)
            {
                continue;
            }
            if ( (arr[i][j]!=-1 && arr[i][j]<=n))
            {
                cout<<arr[i][j]<<" ";
            }
        }
    }
    cout<<endl;
    return 0;
}

- Meraj Ahmed November 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python(Sieve of Eratosthenes)

def main():
	N = 5000
	a = [i for i in range(0,N+1)]

	f=1
	p=2
	while f==1:
		i=2
		while i*p < N+1:
			a[i*p] = 0
			i = i+1
		#print a
		for j in range(p+1,N+1):
			if a[j]!=0:
				p=a[j]
				f = 1
				break
			else:
				f=0
		if f==0:
			break

	for i in a:
		if i!=0 and i!=1:
			print i

if __name__ == '__main__':
	main()

- Vikas November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We need to build a list of prime, for that we dont need to check for each number if its prime or not. Simply check the number if its divisible by initial prime list. Your initial prime list would be {2, 3}, if number is not divisible by all the primes in list then it would be prime as well.
5 is not divisible by 2 & 3 so it's prime and now added to list. For 6 we need to check if its divisible by 2, 3 & 5. If its divisible by any of the prime its not prime.
One more optimization one can do is to ignore all even numbers.

- Aqui February 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool IsPrime(int number, vector<int>& primeList)
{
	bool isPrime = true;

	for(size_t i = 0; i < primeList.size(); ++i)
	{
		if (number % primeList[i] == 0)
		{
			isPrime = false;
			break;
		}
	}

	return isPrime;
}

void PrintPrime()
{
	vector<int> primeList;
	int number;
	primeList.push_back(2);

	cout << "Enter the number : ";
	cin >> number;

	for (int i = 3; i < number; i+=2)
	{
		if (IsPrime(i, primeList))
		{
			primeList.push_back(i);
		}
	}

	for(size_t i = 0; i < primeList.size(); ++i)
	{
		cout << primeList[i] <<",";
	}
}

- Aqui February 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class PrimeNumber {
	public static void main(String[] args) {
		int numberOfPrimes = 125;
		for (int i = 2; i < Integer.MAX_VALUE; i++) {
			if (isPrime(i)) {
				numberOfPrimes--;
				System.out.println(i);
			}
			if (numberOfPrimes == 0) {
				break;
			}
		}
	}

	static boolean isPrime(int n) {
		for (int i = 2; i < n; i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
}

- Solution September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;

public class A2 {

	private static ArrayList<Integer> primes(int n) {
		boolean[] notPrime = new boolean[n + 1];
		notPrime[0] = notPrime[1] = true;
		ArrayList<Integer> results = new ArrayList<Integer>();
		for (int i = 2; i < n; i++) {
			if (notPrime[i] == false) {
				results.add(i);
				for (int j = i * i; j < n; j += i) {
					notPrime[j] = true;
				}
			}
		}
		return results;
	}

	public static void main(String[] args) {
		System.out.println(primes(100));
	}

}

- eng.mohamed.CSD2014 July 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

could be something like this?

void Prime::runPrimes_toN()
{
    //common prime divisors
    //2,3,5,7
    static const int arr[4] = { 2, 3, 5, 7 };

    //generate primes from 2 to N
    std::vector<int> primes;
    for (int i = 2; i <= this->n; i++) {
        bool isPrime = 1;
        for (auto&& p : arr) {
            //if no remainder and counter not = divisor
            //not prime
            if ((i % p == 0) && (i != p)) {
                isPrime = 0;
            }
        }
        if (isPrime == 1) {
            primes.push_back(i);
        }
    }
    for (auto&& c : primes) {
        std::cout << c << std::endl;
    }
}

- Anonymous August 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

http://www.softwareinterviewqna.com/blog/?p=32

- Anonymous March 04, 2009 | 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