Linkedin Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

static public int pickRandom(int[] array, int[] freq) {
        int[] sums = new int[array.length];
        int randValue = 0;
        int sum = 0;
        int randIndex = 0;
        Random random = new Random();

        for (int i = 0; i < array.length; i++) {
            sums[i] = sum + freq[i];
            randValue += random.nextInt(freq[i] + 1);
            sum += freq[i];
            while(randIndex < (array.length - 1) 
                     && randValue >= sums[randIndex] 
                     && randIndex <= i ) {
                randIndex++;
            }
        }
        return array[randIndex];
    }

- kryzoo.m March 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void Output()
        {
          
            var maxFrequency = frequency.Max();

            var rand = new Random().Next(0, maxFrequency);
            for (int i = 0; i < input.Length; i++)
            {
                if (frequency[i] >= rand)
                {
                    Console.Write(input[i]);
                    break;
                }
                
            }


            Console.ReadLine();
        }

- thiru.c March 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void Output()
        {
           
            var maxFrequency = frequency.Max();

            var rand = new Random().Next(0, maxFrequency);
            for (int i = 0; i < input.Length; i++)
            {
                if (frequency[i] >= rand)
                {
                    Console.Write(input[i]);
                    break;
                }
                
            }


            Console.ReadLine();
        }

- thiru.c March 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void Output()
        {
           
            var maxFrequency = frequency.Max();

            var rand = new Random().Next(0, maxFrequency);
            for (int i = 0; i < input.Length; i++)
            {
                if (frequency[i] >= rand)
                {
                    Console.Write(input[i]);
                    break;
                }
                
            }


            Console.ReadLine();
        }

- thirumenin March 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

one can solve this problem in the following way
First Problem

public static int RandomElement(int[] inputArray)
{
	if(inputArray == null || inputArray.Length ==0)
	{
		throw new ArgumentException()
	}
	else
	{
		return inputArray[(new Random()).Next(inputArray.Length)];
	}
}

This is O(1) time and O(1) space complexity method.

For the second problem, we can do following.

public static int FindRandomElement(int[] inputArray, int[] feq)
{
	if(inputArray == null || feq == null || inputArray.Length == 0 || feq;Length == 0 || inputArray.Length != feq.Length)
	{
		throw new ArgumentException()
	}
	else
	{
		int sum = 0;
		for(int i = 0 ; i < feq.Length; i++)
		{
			sum = sum + feq[i];
		}
		int rand = (new Random()).Next(sum);
		for(int i = 0; i < feq.Length;i++)
		{
			if(rand - feq[i] <= 0)
			{
				return inputArray[i];
			}
			else
			{
				rand = rand - feq[i];
			}
		}
	}
}

The time complexity of this algorithm is O(N) and space complexity is O(1). But algorithm has interger overflow problem, which we can solve by following logic

public static int FindRandomElement(int[] inputArray, int[] feq)
{
	if(inputArray == null || feq == null || inputArray.Length == 0 || feq;Length == 0 || inputArray.Length != feq.Length)
	{
		throw new ArgumentException()
	}
	else
	{
		int randsum = 0;
		int j = 0;
		for(int i = 0 ; i < feq.Length; i++)
		{
			randsum = randsum + (new Random()).Next(feq[i]);
			while(j < feq.Length)
			{
				if(randsum - feq[j] > 0)
				{
					randsum = randsum - feq[j];
					j++;
				}
				else
				{
					break;
				}
			}
		}
		while(j < feq.Length)
		{
			if(randsum - feq[j] > 0)
			{
				randsum = randsum - feq[j];
				j++;
			}
			else
			{
				break;
			}
		}
		return inputArray[j];
	}
}

The time complexity of this algorithm is again O(N) and space complexity is O(1), and this does not have interger overflow problem.

- sonesh March 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a[]={1,5,6,8,10,14};
int t=sizeof(a)/sizeof(a[0]);
for(int i =0;i<t;i++)
{
int k=rand()/t;
cout<<a[k]<<"\n";
cout<<
}

- star_akd April 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int GetRandom(vector<int> const &a, vector<int> const &freq)
{
	int idx = -1;
	if (a.size() == freq.size()) {
		int sum = 0;
		for (int i = 0; i < freq.size(); ++i) {
			if (rand() % (sum + freq[i]) >= sum) {
				idx = i;
			}
			sum += freq[i];
		}
	}
	return idx == -1 ? numeric_limits<int>::min() : a[idx];
}

- Alex August 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int pickRandom(int[] array, int[] freq) {

                for (int i=1; i < freq.length; i++) {
                        freq[i]+=freq[i-1];
                }

                int n = new Random().nextInt(1+freq[freq.length - 1]);

                int i=0;
                while (n>freq[i] && i<freq.length) {
                        i++;
                }
                return array[i];
        }

- rm January 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int pickRandom(int[] array, int[] freq) {

                for (int i=1; i < freq.length; i++) {
                        freq[i]+=freq[i-1];
                }

                int n = new Random().nextInt(1+freq[freq.length - 1]);

                int i=0;
                while (n>freq[i] && i<freq.length) {
                        i++;
                }
                return array[i];
        }

- rm January 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

This is a recent LinkedIn on-site question. It's a variant of another question that many have come across before - randomly select a linked list node based on the weights.

The open and follow-up questions are super trivial. Only the extra requirement takes a little probability trick.

public Integer random(int[] array, int[] freq) {
        int totalFreq = 0;
        Integer selected = null;
        Random rand = new Random();

        for(int i = 0; i < array.length; i++) {
            int r = rand.nextInt() * (totalFreq + freq[i]);
            if(r >= totalFreq) {
                selected = array[i];
                totalFreq += freq[i];
            }
        }
        return selected;
    }

Looking for interview questions sharing and mentors? Visit A++ Coding Bootcamp at aonecode.com (select english at the top right corner).
We provide ONE TO ONE courses that cover everything in an interview from the latest interview questions, coding, algorithms, system design to mock interviews.
All classes are given by experienced engineers/interviewers from FB, Google and Uber. Help you close the gap between school and work. Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.
Welcome to email us with any questions.

- aonecoding March 22, 2017 | 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