Epic Systems Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

Based on Sieve of Eratosthenes to find out all the all the primes smaller than input number n, and traverse these primes, while a hash set to store the complementary numbers, once one valid complementary traversed, just return this pair.

- wxliuyizhe October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 vote

package epic;

import java.text.BreakIterator;

public class Goldbach {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Goldback");
		int check_value = 100;
		int i;
		if(check_value%2 != 0){
			System.out.println("Invalid input");
		}
		else{
			for(i=3; i<check_value/2; i=i+2){
				if(isPrime(i) && isPrime(check_value-i)){
					System.out.println("Goldbach of "+ check_value +"= "+ i + " && "+(check_value-i));
				}
			}
		}
	}
	
	public static boolean isPrime(int n){
		int i;
		boolean flag=false;
		if(n%2 == 0){
			return false;
		}
		for(i=3; i<Math.sqrt(n); i=i+2){
			if(n%i ==0){
				return false;
			}
		}
		//System.out.println("Prime num="+n);
		return true;
	}
}

- Anonymous October 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is good, but if the task were to print all possible sums of primes, then the fact that you omit 2 as a prime number will not yield all possible solutions for the case when n = 4 (so we won't see 2 + 2). But this is only an issue when n is 4, because for any larger number than 4 if you subtract 2, the result will be an even number larger than 2, so it wouldn't matter anyway.

- Anonymous December 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

in your isPrime method, 2 is not a prime number.

- Anonymous February 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

Dude...your isPrime() does not works for 9. This is because the loop never runs because of the i<sqrt(9) condition.
Just make the condition as i<=sqrt(n)

- jainAnks February 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 6 vote

public class GoldbachConjecture{


public static void getGoldbachNumbers(int number){
        if(number%2 != 0)
                {
                System.out.println("Please enter an even number.Invalid Input");
                return;
                }
        for(int i=0;i<number/2;i++){

                if(isPrime(i) && isPrime(number-i))
                        System.out.println("Goldbach numbers are :"+i+" and "+(number -i)+" for "+number);

        }


}

public static boolean isPrime(int number){

        for(int i=2;i<number;i++){
                if(number%i == 0)
                        return false;
        }

        return true;
}

public static void main(String[] args){
getGoldbachNumbers(Integer.parseInt(args[0]));

}



}

- somi October 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In getGoldbachNumbers() method, you should also be checking if n>2

- dbhage November 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Only need to check from 2 up to sqrt(number) to determine if number is a prime. If sqrt(number) is not whole then round up to the nearest whole number.

- han October 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You have to check until n/2. Take the example of 10. 5+5.

- jenni October 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thats to determine if the number is sum of primes. I think he or she meant when determining prime number.

- freakpao February 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void printPrimePair(int no){
		//find all the primes eratosthenes algo
		Boolean [] isPrime = new Boolean[no+1];
		for(int i=0;i<no;i++){
			isPrime[i]= true;
		}
		Set<Integer> primeSet = new HashSet<Integer>();
		for(int i=2;i*i<no;i++){
			if(isPrime[i]){
				for(int j=i;j*i<no;j++){
					isPrime[j*i] = false;
				}
			}
		}
		for(int i=2;i<no;i++){
			if(isPrime[i])
				primeSet.add(i);
		}
		//get the goldbach's conjucture 
		for(Integer i:primeSet){
			if(primeSet.contains(no-i)){
				System.out.println("the pair :"+i+" "+(no-i));
				break;
			}
		}
	}

- nm October 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool is_prime(int n){
if (n<=1) return 0;
int m=floor(sqrt(n)+0.5);
for (int i=2; i<m; i++){
if (m%1==0) return 0;}
}
return 1;
}

- Vector October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Slight correction to your code:
int m=ceil(sqrt(n)+0.5); // ex. for 8 m = 4 with ceil() but with floor it is 3 and will not give the output as 5 and 3.

- MeSoRight October 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;
import java.util.*;

public class printassumofprime {

	public static void main(String[] args) {
		Scanner s= new Scanner(System.in);
		System.out.println("Enter a integer");
		int input = s.nextInt();
		printAsSumOfPrimes(input);
	}
	static void printAsSumOfPrimes(int input)
	{
		if(input <2)
		{
			System.out.println("Input is less than 2");
			return;
		}
		if(input%2!=0)
		{
			System.out.println("Input is not divisible by 2");
			return;
		}
		Set<Integer> primenumbers = new HashSet<Integer>();
		for(int i=2;i<input;i++)
		{
			boolean b=true;
			for(int j=2;j<i;j++)
			{
				if(i%j==0)
				{
					b=false;
					break;
				}
			}
			if(b)
			primenumbers.add(i);
		}

		for(int i =input -1;(i>=input/2);i--)
		{
			if(primenumbers.contains(i) && primenumbers.contains(input - i))
			{
				
					System.out.println(input + "="+i+"+"+(input-i));

			}
		}
	}

}

- Anon October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class epic3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 20;
int prime[] = new int[1000];
preprocess(prime,n);
for(int i = 0;i<n/2;i++){
if(prime[i] == 0 && prime[n-i] == 0){
System.out.println(i + " " + (n-i)); break;}
}
}

private static void preprocess(int[] prime, int n) {
// TODO Auto-generated method stub
prime[0] = 1;prime[1] = 1;
for(int i = 2;i<n;i++){
if(prime[i] == 0)
for(int j = i*i;j<n && j > 0;j+=i){
prime[j] = 1;
}
}
}

}

- ajay October 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Goldbach

def Goldbach(n,s):
	if n< 4 or n%2 != 0:
		return 0,0
	for i in s:
		if (n-i) in s:
			return i, n-i
	return 0,0

def findPrimes(s,n):
	for i in range(3,n,2):
		if i >= n:
			return
		if isPrime(i):
			s.add(i)

def isPrime(n):
	for i in range(3, int(n**0.5), 2):
		if n%i == 0:
			return False
	return True

def main():
	n=44444
	
	s = {2}
	findPrimes(s,n)
	print(Goldbach(n,s))

if __name__ == '__main__':
	main()

- . October 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package main
import (
	"fmt"
	"math"
	)

func findPrimes(n int, m map[int]int) {
	for i:=3; i< n; i+=2 {
		if isPrime(i) {
			m[i] = 1
		}
	}
}

func isPrime(n int) bool {
	for i:= 3; i <= int(math.Sqrt(float64(n))); i+=2 {
		if n%i == 0 {
			return false
		}
	}
	return true
}

func GoldBach(n int, m map[int]int) (x, y int) {
	if n < 4 || n%2 != 0 {
		return 0,0
	}
	for k := range m {
		if m[n-k] == 1 {
			return k, n-k
		}
	}
	return 0,0
}

func main(){
	n := 44444
	
	m := make(map[int]int)
	m[2] = 1
	findPrimes(n,m)
	fmt.Println(GoldBach(n,m))
}

- . October 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printPrimePair(int no){
	//find all the primes eratosthenes algo
	Boolean [] isPrime = new Boolean[no+1];
	for(int i=0;i<no;i++){
		isPrime[i]= true;
	}
	Set<Integer> primeSet = new HashSet<Integer>();
	for(int i=2;i*i<no;i++){
		if(isPrime[i]){
			for(int j=i;j*i<n;j++){
				isPrime[j*i] = false;
			}
		}
	}
	for(int i=2;i<no;i++){
		if(isPrime[i])
			primeSet.add(i);
	}
	//get the goldbach's conjucture 
	for(Integer i:primeSet){
		if(primeSet.contains(no-i)){
			System.out.prinln("the pair :"+i+" "+no-i);
		}
	}
}

- nm October 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package p1;
import java.util.HashMap;

public class GoldbachConjecture {
	public static void main(String args[])
	{
		primepair(6);
	}
static void primepair(int num)
{   int c=0;
	int n=num;
	boolean b=true;
	HashMap<Integer, Integer> l=new HashMap();
	if(n%2==0 && n>2)
	{
		for(int i=1;i<n;i++)
		{   int m=0;
			if(isprime(i)&&isprime(n-i)&& m==0)
			{
			System.out.println(i+" "+(n-i));
			m=1;
			}
			
		}
	}
}
static boolean isprime(int number)
{   int v=0;
	int p=number;
	boolean b=false;
  for(int j=2;j<=p/2;j++)	
  {
	  if(p%j==0) 
	  {
	  b=true;
 }
 }
  if(b){
	  return false;}
	  else{ return true;}
}
}

- Davi_singh November 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

'''
Goldbach's conjecture : Every even integer greater than 2 can be expressed as the sum of two primes. 
Write a function which takes a number as input, verify if is an even number greater than 2 and also print atleast one pair of prime numbers.
'''

def goldbach(n):
	if (n > 2 and n % 2 == 0):
		for i in range(2,n/2):
			if is_prime(i) and is_prime(n-i):
				return (i,n-i)
	return None

def is_prime(n):
	for i in range(2,n/2):
		if n % i == 0:
			return False
	return True

print (goldbach(100)) # 3,97
print (goldbach(114)) # 5, 109
print (goldbach(32)) # 3, 29
print (goldbach(2)) # None

- dbhage November 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

for i in range(2,n/2): -> (2,n/2+1)
Think about 6, range(2,3) only check for 2 not 3. Then you miss 6 because it can be divided by two 3s, which are all prime numbers.

- luanmaova March 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A little bit improvement, where we can create prime dictionary to avoid checking already found prime numbers.
primes = {}
def gold_bach(number):
if number%2!=0 or number<=2:
print "Not even or not greater than 2"
for i in range(2, number/2+1):
if primes.has_key(i) and primes.has_key(number-i):
return i, number-i
elif prime(i) and prime(number-i):
primes[i] = 1
primes[number-i] = 1
return i, number-i

- luanmaova March 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class Goldbachs {
    public static void getTwoPrimes(ArrayList<Integer> primes, int number)
    {
        int sum = number;
        for(int i = 0, j = primes.size()-1;i<=j;)
        {
            if(primes.get(i)+primes.get(j)==sum)
            {
                System.out.println("Numbers are: "+primes.get(i)+" and "+primes.get(j));
                return;
            }
            if(primes.get(i)+primes.get(j)<sum)
            {
                i++;
            }
            if(primes.get(i)+primes.get(j)>sum)
            {
                j--;
            }
        }
    }
    public static void main(String args[]){
        ArrayList<Integer> primes = new ArrayList<Integer>();
        primes.add(2);
        primes.add(3);
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int number = Integer.parseInt(sc.next());
        if(number>2)
        {
            if(number%2==0)
                System.out.println("Even number");
            else
                System.out.println("Wrong input");
        }
        else
        {
            System.out.println("Wrong input");
        }
        int count = 0;
        
        for(int i =5;i<number;i=i+2)
        {
            for(int j = 0; j< primes.size(); j++)
            {
                if(i%primes.get(j)==0)
                {
                    count++;
                }
            }
            if(count==0)
            {
                primes.add(i);
            }
            count=0;
        }
        System.out.println(primes);
        getTwoPrimes(primes, number);
    }
}

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

import java.util.*;
public class Goldbachs {
    public static void getTwoPrimes(ArrayList<Integer> primes, int number)
    {
        int sum = number;
        for(int i = 0, j = primes.size()-1;i<=j;)
        {
            if(primes.get(i)+primes.get(j)==sum)
            {
                System.out.println("Numbers are: "+primes.get(i)+" and "+primes.get(j));
                return;
            }
            if(primes.get(i)+primes.get(j)<sum)
            {
                i++;
            }
            if(primes.get(i)+primes.get(j)>sum)
            {
                j--;
            }
        }
    }
    public static void main(String args[]){
        ArrayList<Integer> primes = new ArrayList<Integer>();
        primes.add(2);
        primes.add(3);
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        int number = Integer.parseInt(sc.next());
        if(number>2)
        {
            if(number%2==0)
                System.out.println("Even number");
            else
                System.out.println("Wrong input");
        }
        else
        {
            System.out.println("Wrong input");
        }
        int count = 0;
        
        for(int i =5;i<number;i=i+2)
        {
            for(int j = 0; j< primes.size(); j++)
            {
                if(i%primes.get(j)==0)
                {
                    count++;
                }
            }
            if(count==0)
            {
                primes.add(i);
            }
            count=0;
        }
        System.out.println(primes);
        getTwoPrimes(primes, number);
    }
}

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

public class Solution {
public static boolean isEven(int num){
if(num<=2){
System.out.println("Number less than 3");
}
return num%2==0?true:false;
}
public static boolean isPrime(int num){
for(int i=2;i<=Math.sqrt(num);i++){
if(num%i==0){
return false;
}
}
return true;
}
public static void Goldbach(int num){
if(isEven(num)){
for(int i=2;i<=num/2;i++){
if(isPrime(i)&&isPrime(num-i)){
System.out.println("Pair"+i+","+(num-i));
}
}
}else{
System.out.println("Odd or less than 3");
}

}
public static void main(String[] args){
Goldbach(20);
}
}

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

public class GoldBachConjecture {
	
	private final int check_value;
	
	public GoldBachConjecture(final int check_value){
		this.check_value=check_value;
	}
    public static void main (String args[])
    {
    	GoldBachConjecture goldObj= new GoldBachConjecture(100);
        System.out.println("");
        goldObj.findPair();
    }
	private void findPair() {
		if(check_value%2!=0){
			System.out.println("Invalid input");
		}
		for(int i=3;i<check_value/2;i+=2){
			if(isPrime(i) && isPrime(check_value-i)){
				System.out.println("Goldbach of "+ check_value +"= "+ i + " && "+(check_value-i));
			}
		}

		
		
	}
	private boolean isPrime(int n) {
		if(n%2 == 0){
			return false;
		}
		for(int i=3; i<=Math.sqrt(n); i=i+2){
			if(n%i ==0){
				return false;
			}
		}

		return true;
	}
 
}

- jainAnks February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <string>
#include <iostream>

using namespace std;

int isprime(int n){
    int flag = 0;
    for(int i=2; i<n; i++){
        if(n%i==0){
            ++flag;
            return flag;
        }
    }
    return flag;
}

void goldbach(int n){
    if(n<=2){
        cout<<n<<" not greater than 2"<<endl;
        return;
    }
    if(n%2!=0){
        cout<<n<<" not even"<<endl;
        return;
    }

    for(int i=1; i<((n/2)+1); i++)
    {
        if((isprime(i)==0) && (isprime(n-i)==0))
            cout<<"goldbach pair: "<<i<<" and "<<n-i<<endl;
    }
    return;
}

int main()
{
    goldbach(60);
    return 0;
}

- mohitbar June 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Strong_GoldBach_all_possible_pairs
{
public static void main(int n)
{
if(n%2==1 || n<=2){System.out.println("invalid input");return;}
int n1=0,n2=0;
outer :for(n1=1;n1<n;n1++)
{
if(isprime(n1))
{
for(n2=0;n2<n;n2++)
{

if(isprime(n2)){ if(n1+n2==n && n1<=n2)System.out.println(n1+" + "+n2+" = "+(n1+n2));}
}
}
}
}

private static boolean isprime(long x)
{
long a;if(x==1)return false;
for(a=2;a<=Math.sqrt(x);a++)
{
if(x%a==0)
return false;
}
return true;
}

}

- Saswat Mantri April 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Strong_GoldBach_all_possible_pairs
{
    public static void main(int n)
    {
        if(n%2==1 || n<=2){System.out.println("invalid input");return;}
        int n1=0,n2=0;
        outer :for(n1=1;n1<n;n1++)
        {
        if(isprime(n1))
            {
                for(n2=0;n2<n;n2++)
                {

                   if(isprime(n2)){ if(n1+n2==n && n1<=n2)System.out.println(n1+" + "+n2+" = "+(n1+n2));}
                }
        }
        }
    }
        
        private static boolean isprime(long x)
    {
        long a;if(x==1)return false;
        for(a=2;a<=Math.sqrt(x);a++)
        {
            if(x%a==0)
            return false;
        }
        return true;
    }

}

- Saswat Mantri April 14, 2017 | 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