Epic Systems Interview Question for Software Engineer / Developers






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

import java.util.Calendar;
import java.util.Scanner;

class RandomNumber {
	static long seed = 0;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = Integer.parseInt(scan.next());
		long r = randomn(n);
		scan.close();
		System.out.println(r);
	}

	static private long randomn(int n) {
		Calendar calendar = Calendar.getInstance();
		long reandomnum = 0;
		long m = calendar.getTimeInMillis() + calendar.get(Calendar.SECOND)
				+ (60 * calendar.get(Calendar.MINUTE))
				+ (3600 * calendar.get(Calendar.HOUR));
		if (seed == 0) {
			seed = m;
		}
		reandomnum = (m + seed) % n;
		return reandomnum;
	}
}

- Solution September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is this on the skills round or or site interview?

- Anon September 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

!offtopic | Anon

- Anonymous September 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no response yet! someone please answer this question. the question is really nice.

- Anonymous September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anybody out there

- cunomad September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this question even for n=7 has been asked many times but never a satisfactory solution was provided either by using existing random function of values 0 and 1 or by anyother means...I've never found any solution which generates n random numbers with equal probability but I am curious to know d solution as well....i think loler can answer this one better..

- mayank September 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

LOLer wr r u? we're eagerly waiting for your answer.

- klpd September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Unrelated but nice one:
This is an optimized way of Sieve of Erato used for finding primes, check it out:
http://en.wikipedia.org/wiki/Sieve_of_Atkin

- abe dhakkan September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wtf.....

- dhakkan tu aur tera baap September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think I have the solution. Please tell me if its right or wrong.
To create a random number between 0 and n. Divide the whole range by n. So the question now becomes generating a random number from 0 to 1.
check for n=0, no sense generating numbers from 0 to zero so throw an logical exception.
We need something that is random and keep changing without our intervention.So I opted for the system clock.
DateTime dt = DateTime.Now;
seconds = dt.Second;
// update time if seconds change
if(seconds != dt.Second) {
seconds = dt.Second;
try
{
secondsNum = Convert.ToInt32(seconds);
}
catch (FormatException e)
{
Console.WriteLine("Input string is not a sequence of digits.");
}

x = (1/ secondsNum*n)]
for any n> 0 , mutliply n with [1 - 1/x] could solve our problem.
but if n is very small and tending to zero then 1-1/x would be a negative number.
So I have made
y = 1/(1+x).
now [1-1/y] should gives us a randon number

- Baba sainath September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I made a mistake in the above solution just a typo. x = secondsNum*n
and random number is [1-1/y]n.

- Baba Sainath September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

solution looks fine....but `baba sainath` wouldnt't something like (secondsNum%n) be ok as well ? As in I am fine till the secondsNum thing...but the later part to it looks a bit complicated, even as I said before...seems to be working fine . What say?

- amit September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Baba je :)

can u explain me about this y=1/(1+x) and 1-1/y thing i tried solving it essentially 1-1/y equal to x itself which will be always less then n can u please explain by taking some example values say n=5 and take any random second value

- cunomad September 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we just malloc a piece of mem, and peek what's in there? Is that random enough?

- Roger October 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about this

void printRandom(int n)
DateTime dt = DateTime.Now; 
seconds = dt.Second+60*dt.Minute+3600*dt.Hour;
random=second%n;
cout << random;

- Anonymous December 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What happens when you make two or more calls to printRandom() within the same second? I think you need nanosecond resolution, and potentially another independent variable source of changing value (size of a file in a filesystem listing). Disadvantage here is that there is no seeding, so you cannot generate the same sequence of random numbers again (but that's not been asked for, so nevermind)

- Anonymous March 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{
static long seed = 0;
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int r = randomn(n);
Console.WriteLine(r.ToString());
Console.ReadLine();
}
static private int randomn(int n)
{
DateTime dt = DateTime.Now;
int reandomnum = 0;
long m = dt.Millisecond + dt.Second + 60 * dt.Minute + 3600 * dt.Hour;
if(seed == 0)
{
seed = m;
}
reandomnum = Convert.ToInt32((m + seed) % n);
return reandomnum;
}
}

- Solution with seed April 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
long second =(calendar.getTimeInMillis());
System.out.print(second);
Scanner scan = new Scanner(System.in);
String userInp;
System.out.print("Enter the input");
userInp = scan.next();
long n = Integer.parseInt(userInp);
second=second%10;
System.out.println("The random number is "+second);


}

- moham316 August 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use LCG(linear congruential Generators (ax+b mod n)

- locago September 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sample code --> stackoverflow.com/questions/648976/random-number-generator

- Arun October 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package epic.java;
import java.util.Date;
import java.util.Scanner;
public class GenerateRandomNumber {
	public static void main(String[] args){
		
		Scanner scan=new Scanner(System.in);
		long num=1;
		while(num!=0){
		System.out.println("Enter your num: \n");
		num=scan.nextLong();
		if(num>0)
		 System.out.println("Random Number: "+random(num));	
	}		
		scan.close();
		
	}
	
	public static long random(long num){
		Date date=new Date();
		long timeStamp=date.getTime();
		long ran=timeStamp%num;		
		return ran;
	}
	
}

- Akki November 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{}

- Anonymous May 27, 2016 | 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