Algorithm Interview Questions
- 0of 0 votes
AnswerDesign and implement a sender and receiver system where there can be multiple senders and receivers subscribed to Topics. Each event generated at sender should be received by all receivers subscribed to that topic. Bonus if you can implement group mechanism at receiver side where event is received by one of the receiver in group and received by all groups subscribed to that Topic.
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswerImplement in-memory file system
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersIn memory cache implementation which supports concurrent operations for PUT, GET and DELETE
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersStream of news events come; Need to find top 5 news at any time. use suitable data structure as score of news can dynamically increase or decrease.
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersImplement thread safe generic typed hashmap.
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswerDetermine if a point is inside a 2D convex polygon
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersDesign rubik’s cube and its operation (all rotations and checking final state)
- neer.1304 January 20, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersGiven an Array of N elements and Integer K, write a function that returns true if the sum of any 2 elements of Array is K, false otherwise.
- anonymus January 19, 2017 in United States| Report Duplicate | Flag | PURGE
Facebook Android Engineer Algorithm - 1of 1 vote
AnswersWrite code to decode strings. For example, String str = "3[a2[bd]g4[ef]h]", the output should be "abdbdgefefefefhabdbdgefefefefhabdbdgefefefefh".
My solution is as follows.
- yankeson2013 January 18, 2017 in United Statespublic class StringDecoder { public static void main(String[] args){ String s = "3[a2[bd]g4[ef]h]"; System.out.println(decode(s)); } public static String decode(String s){ if(s == null || s.length()==0) return s; int indexOfFirstNumber = findIndexOfFirstNumber(s); int indexOfFirstBracket = findIndexOfFirstBracket(s); int indexOfClosingBracket = findIndexOfClosingBracket(s, indexOfFirstBracket); if(indexOfFirstNumber == -1) return s; String subStr1 = s.substring(0, indexOfFirstNumber); String subStr2 = decode(s.substring(indexOfFirstBracket+1, indexOfClosingBracket)); String subStr3 = decode(s.substring(indexOfClosingBracket+1, s.length())); int duplicates = Integer.parseInt(s.substring(indexOfFirstNumber, indexOfFirstBracket)); StringBuilder sb = new StringBuilder(); sb.append(subStr1); while(duplicates>0){ sb.append(subStr2); duplicates--; } sb.append(subStr3); return sb.toString(); } public static int findIndexOfFirstNumber(String s){ int index = -1; for(int i=0; i<s.length(); i++){ char c = s.charAt(i); if(c>=47 && c<=58){ index = i; break; } } return index; } public static int findIndexOfFirstBracket(String s){ int index = -1; for(int i=0; i<s.length(); i++){ char c = s.charAt(i); if(c=='['){ index = i; break; } } return index; } public static int findIndexOfClosingBracket(String s, int indexOfBracket){ int index = -1; int numberOfBracket = 1; for(int i=indexOfBracket+1; i<s.length(); i++){ char c = s.charAt(i); if(c == '[') numberOfBracket++; if(c==']'){ numberOfBracket--; if(numberOfBracket==0){ index = i; break; } } } return index; } }
| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm - 0of 0 votes
AnswersGiven a list L of numbers from 0 to n, and another number k = [0-9], find how many times k appears in L. If the target number in L is more than one digit, treat each digit separately. For example, k=0 appears twice in L = [0,10].
- / January 15, 2017 in United States| Report Duplicate | Flag | PURGE
Adobe SDE-3 Algorithm - 3of 3 votes
AnswersGiven a list of system packages, some packages cannot be installed until the other packages are installed. Provide a valid sequence to install all of the packages.
- aonecoding January 15, 2017 in United States
e.g.
a relies on b
b relies on c
then a valid sequence is [c, b, a]| Report Duplicate | Flag | PURGE
Uber Software Engineer Algorithm - 3of 5 votes
AnswersQ: Find the absolute paths to all directories with image files, given a file system that looks like this. The subdirectory is one indent over.
- aonecoding January 15, 2017 in United States/usr /local profile.jpg /bin config.txt dest.png /rbin image.gif /sys /re /tmp pic.jpg ..... ……
| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm - 1of 1 vote
AnswersGiven two strings needle and haywards that contains ASCII characters,write an algorithm to output a list of 0-based indices of the occurances of all anagrams of needle in haystacks
- Learner_Ash January 12, 2017 in United States| Report Duplicate | Flag | PURGE
Amazon Web Developer Algorithm - 0of 0 votes
AnswersGiven a list of list of positive integers, find all pairs of list which are having more than 3 elements overlapping.
- rohitatiit January 10, 2017 in India
What is the complexity.| Report Duplicate | Flag | PURGE
Practo SDE-2 Algorithm - 1of 1 vote
Answersdown vote
- anony January 08, 2017 in India
favorite
Consider the following series:
A := 1
B := A*2 + 2
C := B*2 + 3 and so on...
Write a program that:
-outputs the number corresponding to a given letter;
-given a string of letters like 'GREP', computes the sum of the numbers corresponding to all the letters in the string (i.e., G + R + E + P), as given by the above series; and
-given a large number (that would fit into a standard 32-bit integer), finds the shortest string of letters corresponding to it. You may use a greedy approach for the last part. Compute the values of the numbers corresponding to letters as and when required and DO NOT pre-compute beforehand and store them in a data structure.| Report Duplicate | Flag | PURGE
Amazon Software Developer Algorithm - 0of 0 votes
AnswersWhat is the smallest number *n* by which the given number *x* must be divided to make it into a perfect square?
- NoOne January 08, 2017 in Indian = find_number( x )
| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer Algorithm - 0of 0 votes
Answer2.{{ Query the sum of all the data values in a given rectangle (x1,y1) ~(x2, y2).
- mannurujava January 07, 2017 in United States
int querySum(int x1, int y1, int x2, y2) {}}}| Report Duplicate | Flag | PURGE
Yahoo Backend Developer Algorithm - 0of 0 votes
Answer{{Given a two dimensional grid that stores data as integers with the size of N*M, implement write and query functions which supports:
- mannurujava January 07, 2017 in United States
1. Write the given value to designated coordination (x, y).
void write(int value, int x, int y) {}
}}| Report Duplicate | Flag | PURGE
Yahoo Backend Developer Algorithm - 0of 0 votes
AnswersRunning with Bunnies
- shand January 03, 2017 in United States
====================
You and your rescued bunny prisoners need to get out of this collapsing death trap of a space station - and fast! Unfortunately, some of the bunnies have been weakened by their long imprisonment and can't run very fast. Their friends are trying to help them, but this escape would go a lot faster if you also pitched in. The defensive bulkhead doors have begun to close, and if you don't make it through in time, you'll be trapped! You need to grab as many bunnies as you can and get through the bulkheads before they close.
The time it takes to move from your starting point to all of the bunnies and to the bulkhead will be given to you in a square matrix of integers. Each row will tell you the time it takes to get to the start, first bunny, second bunny, ..., last bunny, and the bulkhead in that order. The order of the rows follows the same pattern (start, each bunny, bulkhead). The bunnies can jump into your arms, so picking them up is instantaneous, and arriving at the bulkhead at the same time as it seals still allows for a successful, if dramatic, escape. (Don't worry, any bunnies you don't pick up will be able to escape with you since they no longer have to carry the ones you did pick up.) You can revisit different spots if you wish, and moving to the bulkhead doesn't mean you have to immediately leave - you can move to and from the bulkhead to pick up additional bunnies if time permits.
In addition to spending time traveling between bunnies, some paths interact with the space station's security checkpoints and add time back to the clock. Adding time to the clock will delay the closing of the bulkhead doors, and if the time goes back up to 0 or a positive number after the doors have already closed, it triggers the bulkhead to reopen. Therefore, it might be possible to walk in a circle and keep gaining time: that is, each time a path is traversed, the same amount of time is used or added.
Write a function of the form answer(times, time_limit) to calculate the most bunnies you can pick up and which bunnies they are, while still escaping through the bulkhead before the doors close for good. If there are multiple sets of bunnies of the same size, return the set of bunnies with the lowest prisoner IDs (as indexes) in sorted order. The bunnies are represented as a sorted list by prisoner ID, with the first bunny being 0. There are at most 5 bunnies, and time_limit is a non-negative integer that is at most 999.
For instance, in the case of
[
[0, 2, 2, 2, -1], # 0 = Start
[9, 0, 2, 2, -1], # 1 = Bunny 0
[9, 3, 0, 2, -1], # 2 = Bunny 1
[9, 3, 2, 0, -1], # 3 = Bunny 2
[9, 3, 2, 2, 0], # 4 = Bulkhead
]
and a time limit of 1, the five inner array rows designate the starting point, bunny 0, bunny 1, bunny 2, and the bulkhead door exit respectively. You could take the path:
Start End Delta Time Status
- 0 - 1 Bulkhead initially open
0 4 -1 2
4 2 2 0
2 4 -1 1
4 3 2 -1 Bulkhead closes
3 4 -1 0 Bulkhead reopens; you and the bunnies exit
With this solution, you would pick up bunnies 1 and 2. This is the best combination for this space station hallway, so the answer is [1, 2].
Test cases
==========
Inputs:
(int) times = [[0, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0]]
(int) time_limit = 3
Output:
(int list) [0, 1]
Inputs:
(int) times = [[0, 2, 2, 2, -1], [9, 0, 2, 2, -1], [9, 3, 0, 2, -1], [9, 3, 2, 0, -1], [9, 3, 2, 2, 0]]
(int) time_limit = 1
Output:
(int list) [1, 2]| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 0of 0 votes
AnswersRandom generate a NxN matrix with only four types of element: 1,2,3,4.
- wtcupup2017 December 30, 2016 in United States
However, no column or row can have same type of element appears 3 times or above continuously (three same type of elements are connected)
ex:
valid:
1 2 1 1
3 1 4 2
1 2 4 2
3 1 2 3
invalid because the first column has element 1 appears three times and all 1s are connected to each other :
1 2 1 3
1 3 4 2
1 2 4 4
2 3 2 2| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm - 0of 0 votes
AnswersIf we have telephone directory with 1,00,000 entries,which sorting algorithm is best?
- Ankita December 29, 2016 in India| Report Duplicate | Flag | PURGE
Algorithm - 0of 0 votes
Answersgiven the array of n elements can we output in sorted order k elements following median in sorted order in time O(n+klogk)
- Ankita December 28, 2016 in India| Report Duplicate | Flag | PURGE
Algorithm - 1of 1 vote
AnswersAn interesting question asked in Google’s phone interview : suppose a row of parking lot with n spots, one of them is empty and n-1 spots are occupied with cars. Only one operation is allowed: move one car from its position to the empty spot. Given a initial order of cars and a final order, output steps needed to convert initial order to final oder with that operation.
- wtcupup2017 December 28, 2016 in United States
Follow up: Minimize steps needed.
ex:
{1 2 3 -1 4 5}
move car 1 to empty spot(denoted as -1) will make it {-1,2,3,1,4,5}
push 1 to the output list because you move car 1 to the empty spot
suppose you have a initial order {1 2 3 -1 4 5} and a final order {5,1,-1,3,2,4}, you need to transfer {1 2 3 -1 4 5} to {5,1,-1,3,2,4}, push each car moved into a output list.| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm - 2of 2 votes
AnswersGiven points on a plane like (0,0), (1,0), (0,1), (1,1), (0,2), (2,2), (1,2). How many rectangles can be formed ?
- solveIt December 26, 2016 in United States| Report Duplicate | Flag | PURGE
Bloomberg LP Software Engineer / Developer Algorithm - 0of 0 votes
AnswersWrite code for implementing Binary Search algorithm.
- kreetanshu December 26, 2016 in India| Report Duplicate | Flag | PURGE
Amazon Testing / Quality Assurance Algorithm - -1of 1 vote
AnswersGiven a few pairs of names in the order child, father. The input is a person name and level number. The output should be the number of children in that particular level for the person given.
- bharadwajdaya December 19, 2016 in India for Theriyathu
Example:
Input:
[
{Ram, Syam},
{Akil, Syam},
{Nikil, Ram},
{Subhash, Ram},
{Karthik, Akil}
];
Syam 2
Output: 3 (Syam has Ram and Akil in level 1 and in level 2 he have Nikil, Subhash, Karthik. So the answer is 3).| Report Duplicate | Flag | PURGE
Zoho Senior Software Development Engineer Algorithm - -4of 4 votes
AnswersGiven a string and dictionary of words, form a word by removing minimum number of characters. Characters can be removed in-order only.
- rd22 December 14, 2016 in United States| Report Duplicate | Flag | PURGE
Google Software Developer Algorithm - 0of 0 votes
AnswersGiven a set find its power set. (This question is from CTCI) Interviewer then discussed the complexity of my solution. He asked me to explain him how the complexity is 2^n? As per my solution, I was iterating over an arraylist which contained the set elements so the size of list was getting doubled in every iteration. Hence the complexity (2^0 + 2^1 + 2^2 +.....+2^n-1 = 2^n)
- 256.cool December 14, 2016 in United States| Report Duplicate | Flag | PURGE
Bloomberg LP Software Engineer / Developer Algorithm - 0of 0 votes
AnswersGiven a string, add some characters to the from of it so that it becomes a palindrome. E.g.1) If input is "abc" then "bcabc" should be returned. 2) input -> "ab" output -> "bab" 3) input -> "a" output -> "a"
- 256.cool December 14, 2016 in United States| Report Duplicate | Flag | PURGE
Bloomberg LP Software Engineer / Developer Algorithm