Google | Onsite
Anonymous User
3194

I was asked this question in the coding interview (second round)

  1. Design a game 5x5. Given getRandom(start, end) function to generate all random numbers from 1 to 75

[[1, 5, 2, 8, 13] number random from 1 to 15
[19,17,16,25] number random from 16 to 30
[35,32,43,41,39] 31 to 45
[etc...]

Create a void function which generate the game and return 2D array which contains game

no input (void function)
output will be 2D array

my solution was:

vector<vector> generateGame()
{
int m = 5;
int n = 5;

vector<vector<int>> ans(m, vector<int>(n,0));
unordered_set<int> set;

int start = 1;
int end = 15;

for(int i = 0; i < m; i++)
{
	for(int j = 0; j < n; j++)
	{
		int number = getRandom(start, end);
		while(set.count(number)) number = getRandom(start, end);
		ans[i][j] = number;
		set.insert(number)
	}
	start += 15;
	end += 15;

}
return ans;
}

I'm not sure there are a better solution for this question?

Comments (10)