Google Interview Question for Software Developers


Country: United States




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

In CPython 2:

from random import randint as ri

def ri_odd(low, high):
	assert low <= high and (low % 2 == 1 or high - low > 0)
	return ri(low / 2, (high - 1) / 2) * 2 + 1

Why does this work?

I assume that it works like randint so "high" is included in the range. We don't want low to be bigger than high and if low is equal to high we want it to be an odd number. That's what the assertion does.

Now for the limits in randint(ri). If low is odd as in 2 * k / 1 then low / 2 = k so it's good. if low is even as in 2 * k then low / 2 = k so it's also good. If high is odd as in 2 * k + 1 then (high - 1) / 2 = k so it's good and if it is even as in 2 * k then (high - 1) / 2 = k - 1 which is also good since 2 * k - 1 = 2 * (k - 1) + 1 is the largest odd number in this range.

- Generating odd random numbers in CPython 2 April 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Interesting problem. There can be at least 2 ways to get it done.
The trivial one:

def getRandomOdd( min, max){
  r = random()
  while ( (n = r.num(min,max)) % 2 == 0 );
  n // return n 
}

The problem is - not really uniform, in some sense. To generate uniform, we use a reverse map:
y_min, y_max : such that
2 * y_min + 1 -> min
2 * y_max + 1 -> max
If min, max are not that way, we change them by something. Now we have a cleaner solution:

def getRandomOdd( min, max){
  r = random()
  y_min = min/2 
  if ( 2 /? max ){ max - 1 }
  y_max = max/2 
  2 * r.num( y_min, y_max ) + 1 
}

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

private static Object generateOddRandomNo_2(int min, int max) {
int randomNumber = new Random().nextInt((max - min) + 1) + min;
return randomNumber%2 != 0 ? randomNumber : generateOddRandomNo_2(min, max);
}

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

private int generateOddRandomNo(int min, int max) {
		int randomNumber = new Random().nextInt((max - min) + 1) + min;
		return randomNumber%2 != 0 ? randomNumber : generateOddRandomNo_2(min, max);
	}

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

C++ solution

int getRandomOdd(int left, int right)
{
  // Exclude right-most odd number
  right = right % 2 == 1 ? right - 1 : right;

  // Error checking
  if (left >= right) return 0;

  // Divide out even numbers and shift to the left
  int leftRng = rand() % ((right - left + 1) / 2) - 1;

  // Check for left even shift
  int leftShift = left % 2 == 0 ? 1 : 0;

  // Shift it back to the right and expand to all odd numbers
  return left + leftShift + 2 * (leftRng + 1);
}

- Josh May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

New to this platform .writing a c program instead of function .can anyone tell if my code is correct or not.

#include<stdio.h>
#include<time.h>
int main()
{
    int min,max,a[10],b[10],i,var1=0,diff;
    int j,max_arr;
    time_t t;
    printf("enter min ad max");
    scanf("%d %d",&min,&max);
    printf("enter any number between min and max excluding max");
    diff=max-min;
    printf("\ndiff is %d",diff);
    for(i=0;i<diff;i++)
    {
        scanf("%d",&var1);
        if(var1>=min && var1 <max)
        {
            a[i]=var1;

        }
        else
        {
            i--;
        }
        printf("i is %d",i) ;
    }
    for(i=0;i<diff;i++)
    {
        printf("\n b is %d",a[i]);
    }
    for(i=0;i<diff;i++)
    {
        if(a[i]%2!=0)
        {
            b[j]=a[i];
            j++;
        }
    }
    srand((unsigned) time(&t));
    i=rand()%j;
    printf("rand no is %d",b[i]);


}

- rp July 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int RandomOdd(int min, int max)
{
    int delta = max - min;
    if (delta <= 0)
    {
        return -1;
    }
    int odd_numbers_count = delta / 2 + (min % 2 != 0 ? 1 : 0);
    int rnd = rand() % odd_numbers_count;
    return min + rnd * 2 + (min % 2 == 0 ? 1 : 0);
}

- Alex October 26, 2018 | 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