Google Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

There are two problems with this solution.
1.It's not a random number generator. It means that the number you generated is not random. It depends on the time.
2. The reason why you always get 0 firstly is that any positive mod 1 is 0. In your case, you have the first number mod i where i is always 1 initially.

The corrent way to generate a ranom number is to use Linear congruential. You can check that out.

- ravio June 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Check out Xorshift (e.g. in wikipedia) for generating random numbers

- Pooyo June 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here a possible solution in Swift:

func randomizer(num:Int, addNum: Int) -> Int {
    if num > 0 {
        var tmp:Int = NSDate.timeIntervalSinceReferenceDate().bridgeToObjectiveC().integerValue;
        return (tmp % num) + addNum;
    } else {
        return 0;
    }
}
// range 100-199
println(randomizer(100,100));

- BerMuc June 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

/**
	 * @Description: random number generator
	 * @param seed 
	 * @param num how many numbers to generator
	 */
	static void randomGenerator(int seed, int num) {
		while (num-- > 0) {
			String md5 = getMD5(String.valueOf(seed));
			int random = 0;
			for (int i = 0; i < md5.length(); i++)
				random += md5.charAt(i) - 'A';
			System.out.println(random);
			seed++;
		}
	}

	/**
	 * @Description: generate md5
	 * @param input
	 * @return
	 */
	static String getMD5(String input) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md.digest(input.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			String hashtext = number.toString(16);
			return hashtext;
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}

}

- fang-l10 June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, every time the O/p is constant.

- vrajendra.singh.mandloi June 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

em, what is O and p ?

- fang-l10 June 23, 2014 | Flag
Comment hidden because of low score. Click to expand.


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