Google Phone screen
Anonymous User
1043
Apr 04, 2022

Randomizer

Given a class like the following:

class Randomizer {

	int n, k;
	Randomizer(int n, int k) {
		this.n = n;
		this.k = k;
}

 	int getRandom() {
		

	}
}

Implement the getRandom method. getRandom returns a random number in the range [1,n] but a number once generated should not be generated again in next k turns. For example:

n = 5
k = 3

getRandom() returns 2 (assume)
getRandom() return x
getRandom() returns y
getRandom() returns z
getRandom() returns w

now as per the constraint x, y and z cannot be 2. But w can be 2. Similarly y, z and w cannot be x.

Can someone help me?

Comments (8)