There are 36 horses. We have to find out the fastest 3 horses. In one race, maximum 6 horses can run. How many races are required in minimum to get the result without using a stopwatch?
We can first do 6 races by taking 6-6 horses and get the fastest horse in each race.
A1, A2, A3, A4, A5, A6
B1, B2, B3, B4, B5, B6
C1, C2, C3, C4, C5, C6
D1, D2, D3, D4, D5, D6
E1, E2, E3, E4, E5, E6
F1, F2, F3, F4, F5, F6
Here A1>A2>A3>A4>A5>A6 and same way for others.
Now let take the fastest horse in each race and then have one race between them. Let result be A1>B1>C1>D1>E1>F1.We get the fastest horse(A1).
Now we need to find 2nd and 3rd fastest horses. Available horses for the 2nd position are B1 and A2 and for the 3rd position are A3, B2, and C1. All other horses cannot be in top 3. Let have one race between these horses and top 2 will be 2nd and 3rd fastest horse respectively.
So a total of 8 races is required.
Which data structure is best suited to find the median of running stream of numbers (online algorithm )
The idea is to use max heap and min heap to store the elements of left half lower than current median and right half greater than current median respectively. The number of elements in heaps differs utmost by 1 element for every input. When both heaps contain the same number of elements, we pick average as median, else we select median from the heap containing more elements.
For more detail refer here.
There are N people in a party, they might or might not know each other names.
There is a celebrity in the group, celebrity does not know anyone and all people know the celebrity.We can only ask questions like “does A know B?”. (Celebrity problem)
What is the worst case time complexity of the optimal solution to find the celebrity?
Let’s say we ask “does A know B?”. If A knows B, A can not be a celebrity.If A does not know B then B can not be a celebrity. So after each question, we can reject one person. Thus we need to ask a maximum of n questions to correctly figure out the celebrity.
For more detail refer here.
Given 2 strings A and B, return a string in which characters from A and B are filled alternatively in order same as in original strings beginning with a character from A, example A = “Hello”, B= “Bye” then output should be “HBeylelo”. (|A|,|B|<25000)
Create a new empty string. Run a loop min(|A|,|B|) time and append ith character of A and then ith character of B to the answer string.At last, append the remaining portion of string A or B to answer string.
Given a number N find the number of ways to write N as a sum of two or more positive consecutive integers for if N is 7 example there is just 1 way, that is 3+4. (0<N<10^12)
Suppose we start our summation series from 1 i.e. 1+2+3+…..
Max no. of numbers whose summation will be less than or equal to N: floor(X)
So N as a sum of positive consecutive integers will at max consist of X numbers.
Here we can check each possibility k(assume) between 2 and X (both inclusive) and to check we can use the concept of A.P. as the summation series is an A.P whose sum is N.
We know the no. of term in A.P, n: k
Sum of A.P, S: N
Common difference, d: 1
We can easily calculate the first term a, using formula:
S=n/2*(2a + (n-1)d)
If a is an integer then N can be represented as a sum of k continuous integers, otherwise, it is not possible. Count the number of occurrences when a is an integer and that is our answer.
Time Complexity: Loop run X time which is O(sqrt(N)) and in each iteration, we take O(1) time. So overall time complexity is O(sqrt(N)).
P.S: Written test also contained questions related to ML and Quant.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.