## Oracle Interview Question for Software Developers

• 0

Country: India

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

Using bitwise OR:

``````public int rand75(){
return rand50() | rand50();
}``````

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

Generic pseudo-code, not intended to compile. Not tested, but I think it should work.

``````function int myRandom() {
int roll1 = originalRandom();
int roll2 = originalRandom();

// Only 4 possible combinations, with equal probabilities:
// roll1 = roll2 = 0
// roll1 = roll2 = 1
// roll1 = 0; roll2 = 1
// roll1 = 1; roll2 = 0

if (roll1 == 0 && roll2 == 0)  // 25% probability
return 0;
else  // 75% probability
return 1;
}``````

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

Considering rand50() is defined, then rand75() would be

``````char rand75() {
return !(rand50() && rand50());
}``````

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

``````#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int rand50(){
srand(time(NULL));
int randInt= rand(); // randInt is between 0 and RAND_MAX
(x%2)==0? return 1 : return 0;
}

int rand75(){
int x = rand50();   // x is now one of {0, 1}
x = x << 1;           // x is now one of {00, 10}
x = x ^ rand50();  // x is now one of {00, 01, 10, 11} in binaries
(x>0)? return 1 : return 0;
}``````

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

Pretty rusty with probabilities, so let me know your thoughts if this works out to be 75%
My first test, I rolled 12 times, got 9 rolls that returned 1, 3 rolls that return 0, gave me exactly: 9/12 = 75%
Sample size should be larger, so I should run more tests.

``````/* The lowest number of rolls to get a combination of 4 such that we can pick 3 out of 4
to give the 75% probability is through two rolls:

Roll Results
1: 0, 0
2: 0, 1
3: 1, 0
4: 1, 1

Pick 1 - 3 to return 1 for 75%
Pick 4 to return 0 for 25% probability
*/

#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int random50()
{
int result = rand() % 2;
return result;
}

int random75()
{
const int size = 3;
int valuesFor1[size] = { 0, 1, 10 };

// First roll is for the 10's digit place
int roll1 = random50();
int roll2 = random50();
int result = (roll1 * 10) + roll2;
for (int i = 0; i < size; ++i)
{
if (result == valuesFor1[i])
{
return 1;
}
}
return 0;
}

int main()
{
srand (time(NULL));
int value = random75();
cout << value << endl;
return 0;
}``````

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

Generic pseudo-code, not intended to compile. Not tested, but I think it should work.

``````function int myRandom() {
int roll1 = originalRandom();
int roll2 = originalRandom();

// Only 4 possible combinations, with equal probabilities:
// roll1 = roll2 = 0
// roll1 = roll2 = 1
// roll1 = 0; roll2 = 1
// roll1 = 1; roll2 = 0

if (roll1 == 0 && roll2 == 0)  // 25% probability
return 0;
else  // 75% probability
return 1;
}``````

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

Generic pseudo-code, not intended to compile. Not tested, but I think it should work.

``````function int myRandom() {
int roll1 = originalRandom();
int roll2 = originalRandom();

// Only 4 possible combinations, with equal probabilities:
// roll1 = roll2 = 0
// roll1 = roll2 = 1
// roll1 = 0; roll2 = 1
// roll1 = 1; roll2 = 0

if (roll1 == 0 && roll2 == 0)  // 25% probability
return 0;
else  // 75% probability
return 1;
}``````

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

bool rand75(){
if(rand50()==1)
return1;
return rand50();
}

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

``````from random import randint

# given function
def rand_50() :
return randint(0,1)

# requested function
def rand_75() :
if (rand_50() + rand_50()) == 2 :
return 0
else :
return 1

# test distribution over n outcomes
def test(n) :
outcome = 0
for i in range(0,n) :
outcome = outcome + rand_75()
p_1 = outcome / n
p_0 = 1 - p_1
print("p_0= %s, p_1= %s " % (p_0, p_1))

test(10000)``````

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

``````import random

def rand_0_5():
return 1 if random.random() >= 0.5 else 0

def rand_0_75():
return 0 if not (rand_0_5() + rand_0_5()) else 1``````

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

``````public int random75(){
int ans = random50(); // 50%
if(ans == 0){
ans = random50();   //for 0 50% / 2 = 25%
}
return ans;
}``````

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.