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

}

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])
}
}

}``````

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

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

// 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);
}
}
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;
}
}

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

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

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

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.