Google Interview Question for SDE1s


Country: United States




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

Using sieve of erotospenes, complexity O(nloglogn):

package main

import (
	"fmt"
	"math"
	"math/rand"
)

func main() {
	primes := findPrimes(225, 350)
	r := rand.New(rand.NewSource(7))
	fmt.Printf("random %d", primes[r.Intn(len(primes))])
}

func initArray(max int) []int {
	toReturn := make([]int, max+1)
	for i := 0; i < max; i++ {
		toReturn[i] = i + 1
	}

	return toReturn
}

func findPrimes(min, max int) []int {
	input := initArray(max)
	sqrtN := int(math.Sqrt(float64(max)))
	for seed := 2; seed < sqrtN; seed++ {
		for i := seed; i < max; i++ {
			if input[i] != -1 && input[i]%seed == 0 {
				input[i] = -1
			}
		}
	}

	toReturn := make([]int, 0)
	for i := min - 1; i < max; i++ {
		if input[i] != -1 {
			toReturn = append(toReturn, input[i])
		}
	}

	return toReturn
}

- Marcello Ghali April 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private int randomPrime(int min, int max) {
		int random = (int) (Math.random() * max);
		if(random < min) {
			random = randomPrime(min, max);
		}
		if(!isPrime(random)) {
			random = randomPrime(min, max);
		}
		return random;
	}
	
	private boolean isPrime(int random) {
		boolean isPrime = !(random % 2 == 0);
		if(!isPrime) {
			return false;
		} else {
			for(int i = 3; i*i < random; i++) {
				if(random % i == 0) {
					return false;
				} else {
					isPrime = true;
				}
			}
		}
		return isPrime;
	}

- Hiren Parmar April 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RandomPrimeNumbers {

// generate randmon prime number betweeb min and max
public static void main(String args[]) {
int min = 1;
int max = 11;
List<Integer> list = new ArrayList<>();
for(int i =min; i<=max; i++) { // O(max)
if(i != 1 && isPrime(i)) {
System.out.println(i);
list.add(i);
}
}
Random r = new Random(System.nanoTime());
System.out.println("Randmon prime :"+ list.get(r.nextInt(list.size())));
}
private static boolean isPrime(int n) {
for(int i = 2; i*i<=n;i++){ // this O(sqrt(n)) time complexity
if(n%i == 0)
return false;
}
return true;
}
}

- Rj April 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RandomPrimeNumbers {

	// generate randmon prime number betweeb min and max
	public static void main(String args[]) {
	int min = 1;
	int max = 11;
	List<Integer> list = new ArrayList<>();
	for(int i =min; i<=max; i++) { // O(max)
		if(i != 1 && isPrime(i)) {
			System.out.println(i);
			list.add(i);
		}
	}
	Random r = new Random(System.nanoTime());
	System.out.println("Randmon prime :"+ list.get(r.nextInt(list.size())));
	}
	private static boolean isPrime(int n) {
		for(int i = 2; i*i<=n;i++){ // this O(sqrt(n)) time complexity
			if(n%i == 0)
				return false;
		}
		return true;
	}
}

- Rj April 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RandomPrimeNumbers {

	// generate randmon prime number betweeb min and max
	public static void main(String args[]) {
	int min = 1;
	int max = 11;
	List<Integer> list = new ArrayList<>();
	for(int i =min; i<=max; i++) { // O(max)
		if(i != 1 && isPrime(i)) {
			System.out.println(i);
			list.add(i);
		}
	}
	Random r = new Random(System.nanoTime());
	System.out.println("Randmon prime :"+ list.get(r.nextInt(list.size())));
	}
	private static boolean isPrime(int n) {
		for(int i = 2; i*i<=n;i++){ // this O(sqrt(n)) time complexity
			if(n%i == 0)
				return false;
		}
		return true;
	}
}

- rj April 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is some code with python

# write a function that randomly return a random prime
# number in range [min, max)
# public int getRandom (int min, int max){}
# April 2017
import random


def getRandom(min, max):
    if  min > 1:
        rangeRover = random.randint(min, max)
        for i in range(min,max):
            if rangeRover % i == 0:
                return False
            else:
                return rangeRover
    else:
        return False

print('my random numbers based on given range', getRandom(12,407))

- joneschris903 May 19, 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