Algorithm Interview Questions
- 0of 0 votes
AnswersThere is dedicated Samsung software for coding test the question is given below:
- pbox February 09, 2017 in India
There is one spaceship. X and Y co-odinate of source of spaceship and destination spaceship is given. There are N number of warmholes each warmhole has 5 values.
First 2 values are starting co-ordinate of warmhole and after that value no. 3 and 4 represents ending co-ordinate of warmhole and last 5th value is represents cost to pass through this warmhole. Now these warmholes are bi-direction.
Now the to go from (x1,y1) to (x2,y2) is abs(x1-x2)+abs(y1-y2).
The main problem here is to find minimum distance to reach spaceship from source to destination co-ordinate using any number of warm-hole. It is ok if you wont use any warmhole.| Report Duplicate | Flag | PURGE
Samsung Software Engineer Algorithm - 0of 0 votes
Answerslist1 -->aaa,bbb,ddd,xyxz,...
- codemarathon February 09, 2017 in United States
list2-->bbb,ccc,ccc,hkp,..
list3> ddd,eee,,ffff,lmn,..
Inside a list the words are sorted
I want to remove words which are repeated across the list and print in sorted order
If the words are repeated in same list its valid.
In the above case
it should print aaa-->ccc-->ccc-->eee--->fff-->glk-->hkp-->lmn-->xyxz| Report Duplicate | Flag | PURGE
Amazon Software Engineer Algorithm - 2of 2 votes
AnswersWrite a function that receives a position in 2 dimensional (x,y) array, which was initially initialized with 'o' (signals "water"), the function changes the value/state of that position to 'x' (signals "land") and returns the number of isles in the board.
- vesh February 08, 2017 in Irland
For example, for 3x3 board, it will initially look like the following:
o o o
o o o
o o o
After calling the function with the position (1,2), the board will look like the following:
o o x
o o o
o o o
and the functions returns 1
An isle is defined as 'x' surrounded horizontally and vertically with 'o'
In the following board there is only one isle
o o o
o x x
o x o| Report Duplicate | Flag | PURGE
Google Site Reliability Engineer Algorithm - 0of 0 votes
AnswersFind top n cities which got most orders. For example, amazon got a list of orders, and these orders will be shipped to different cities.
- yankeson2013 February 08, 2017 in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer Algorithm - 1of 1 vote
AnswersI got my interview yesterday and the problem they asked me was: Giving a method intToEnglish that receives an int as a parameter, how do you return its representation in english words. The number can be of any size but no more than around 2 billion since the parameter is an int 2ˆ32
- jorgevalbuena56 February 08, 2017 in United States| Report Duplicate | Flag | PURGE
Facebook Android Engineer Algorithm - 4of 4 votes
Answers# There's a room with a TV and people are coming in and out to watch it. The TV is on only when there's at least a person in the room.
- aonecoding February 08, 2017 in United States
# For each person that comes in, we record the start and end time. We want to know for how long the TV has been on. In other words:
# Given a list of arrays of time intervals, write a function that calculates the total amount of time covered by the intervals.
# For example:
# input = [(1,4), (2,3)]
# > 3
# input = [(4,6), (1,2)]
# > 3
# input = {{1,4}, {6,8}, {2,4}, {7,9}, {10, 15}}
# > 11| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm - 0of 0 votes
AnswersI recently had a take home assignment for a optimization engineer position at FB. I got 24 hours to finish it however it was suggested to finish under 90 minutes. The problem was to calculate the dot product of sparse matrix (optimize for speed). I wrote following code but got dinged. Not sure what's wrong to not clear even first stage!
- Ad February 07, 2017 in United States for Advertisement optimization#include <iostream> #include <string> #include <map> #include <sstream> using namespace std; class SparseVector{ private: map<int, double> m_map; int m_size; public: SparseVector(int size){ this->m_size = size; this->m_map = *new map<int, double>(); } void setValue(int i, double value){ if( i < 0 || i > m_size) return; if(value == 0.0){ map<int, double>::iterator it = m_map.find(i); if(it != m_map.end()) m_map.erase(it); } else m_map[i] = value; } double getValue(int i){ if( i < 0 || i > m_size) return 0.0; else { map<int, double>::iterator it = m_map.find(i); if(it != m_map.end()) return it->second; else return 0.0; } } int getSize(){ return m_size; } static double s_dotProduct(SparseVector a, SparseVector b){ if (a.m_size != b.m_size) return 0.0; double sum = 0.0; if (a.m_map.size() <= b.m_map.size()) { for (map<int, double>::iterator it = a.m_map.begin() ; it != a.m_map.end(); it++) if ((b.m_map.find(it->first)) != b.m_map.end()) sum += a.getValue(it->first) * b.getValue(it->first); } else { for (map<int, double>::iterator it = b.m_map.begin() ; it != b.m_map.end(); it++) if ((a.m_map.find(it->first)) != a.m_map.end()) sum += a.getValue(it->first) * b.getValue(it->first); } return sum; } template <typename T> static string s_ToString(T val) { stringstream stream; stream << val; return stream.str(); } static string s_printSparseVector(SparseVector a) { string s = ""; for (map<int, double>::iterator it = a.m_map.begin(); it!= a.m_map.end(); it++) s += "(" + s_ToString(it->first) + ", " + s_ToString(it->second) + ") "; return s; } }; int main(int argc, char *argv[]){ int lenA = 0, lenB = 0, setValA = 0, setValB = 0; cout << "Enter length for Sparse vector A : " << endl; cin>> lenA; cout << "Enter length for Sparse vector B : " << endl; cin>> lenB; SparseVector v1 = SparseVector(lenA); SparseVector v2 = SparseVector(lenB); double y; int x; cout << "Enter number of set values for Sparse vector A : " << endl; cin >> setValA; cout << "Enter the set Index, Value pair in separate lines" << endl; for(int i = 0; i < setValA; i++){ cin >> x; cin >> y; v1.setValue(x, y); } cout << "Enter number of set values for Sparse vector B : " << endl; cin >> setValB; cout << "Enter the set Index, Value pair in separate lines" << endl; for(int j = 0; j < setValB; j++){ cin >> x; cin >> y; v2.setValue(x, y); } cout <<"Sparse Vector1 = " << SparseVector::s_printSparseVector(vectorA) << endl ; cout <<"Sparse Vector2 = " << SparseVector::s_printSparseVector(vectorB) << endl; cout <<"Dot product of two sparse vectors is = " << SparseVector::s_dotProduct(vectorA, vectorB) << endl; return 0; }
| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm - 0of 0 votes
AnswersArray consist of -1 and 1, Find count of all sub-arrays where sum = 0.
- itmvikastiwari February 06, 2017 in India
Ex. [-1,1,-1,1]
Ans : 4
[-1,1] [1,-1],[-1,1],[-1,1,-1,1]
Ex. [-1,-1,1,1]
Ans : 2 [-1,1][-1,-1,1,1]| Report Duplicate | Flag | PURGE
Snapdeal Tech Lead Algorithm - -1of 1 vote
AnswersWrite a iterator wrapper to remove duplicates from collections without using temporary storage.
- dmh February 04, 2017 in United States
For Example:
ArrayList A = {RAT,CAT,BAT}
ArrayList B = {RAT,CAT,MAT}
ResultIterator itr = new ResultIterator();
itr.next() should display {RAT,CAT,BAT,MAT}
Program skeleton:
class ResultIterator {
ResultIterator(iterator itr1, iterator itr2) {
}
bool hasnext {
// implement this method
}
E next() {
// implement this method
}
}| Report Duplicate | Flag | PURGE
Amazon Software Developer Algorithm - -1of 1 vote
AnswersYou are given a series of moves as a string, such as ">^V<^". You need to output a list with all the series of moves that represent a loop if they were made by a robot for example.
- Ray February 01, 2017 in United States
A loop is defined as a series of moves that lead to the same position, for example "><" or "^V".
In the case of "^>V<^" the output should be ["^>V<", "^>V<"], essentially the last move generates a new loop because it leads to a position that was visited before and that naturally can be followed up to the same position and describe a loop.| Report Duplicate | Flag | PURGE
Software Engineer Algorithm - 3of 7 votes
AnswersConsider a hotel where the guest is checked in and check out. Find a day when the maximum number of guests stay in a hotel.
- itsvks February 01, 2017 in Netherlands
example:
Input :
[
{check-in : 1, check-out 4},
{check-in : 2, check-out 5},
{check-in : 10, check-out 12},
{check-in : 5, check-out 9},
{check-in : 5, check-out 12}
]
Output : 5| Report Duplicate | Flag | PURGE
Booking.com Software Engineer / Developer Algorithm - 2of 2 votes
AnswersGiven number N, Find the least number of perfect square number sum needed to get N.
- itsvks February 01, 2017 in Netherlands
Example :
n=5 (4+1) i.e. 2
n=7 (4+1+1+1) i.e. 4
n=12 (4+4+4) i.e 3
n=20 (16+4) i.e. 2| Report Duplicate | Flag | PURGE
Booking.com Software Engineer / Developer Algorithm - 1of 1 vote
AnswerYou are given an array A[] with n elements. You are given S[], E[] and H[] array with each M elements.
- Richa Tibrewal February 01, 2017 in India
Apply an operation such that all the elements from A[ S[i] ] and A[ E[i] ] will be less than H[i].
Example :
given array A[] = [2,4,8,6,7]
S[0] = 1
E[0] = 4
H[0] = 5
So, after doing an operation, the resulting array is A[] = [ 2,4,5,5,5]
Thus, u need to do the same thing for all i.
After doing this, find out the maximum element in the array.| Report Duplicate | Flag | PURGE
Directi SDE1 Algorithm - 3of 3 votes
AnswersGiving a string and an string dictionary, find the longest string in dictionary which can formed by deleting some characters of the giving string.
- IKnowThings January 28, 2017 in United States
eg:S = abpcplea, Dict = {ale, apple, monkey, plea}, the return "apple"
I was thinking of the following approach,
Build a Trie for all words in the Dict - O( n * k) where k is the longest string in the dict
For each character c, in S check if there is a word in Trie that starts with it and has the letters that appear in S after c. We can short circuit based on remaining characters and the length of longest string found so far.
This should take O( N * k) where N is length of S.| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm - 6of 6 votes
Answers/**
- aonecoding January 27, 2017 in United States
Given many coins of 3 different face values, print the combination sums of the coins up to 1000. Must be printed in order.
eg: coins(10, 15, 55)
print:
10
15
20
25
30
.
.
.
1000
*/| Report Duplicate | Flag | PURGE
Facebook Software Developer Algorithm - 2of 4 votes
AnswersFind all comments in the Java (it could be Python or any other language of your choice) codes that’s parsed in as a string.
- aonecoding January 27, 2017 in United States
You may assume the codes given is valid.
Input is a single string, e.g.
String codes =
“/* file created by aonecode.com\\n” +
“ welcome to the tech blog*/ \\n” +
“//main method\\n” +
“public static void main(String[] args) { \\n“ +
“ System.out.println(“//welcome”); //output\\n” +
“}”
Output is a list of strings
List<String> ret =
[
“ file created by anecode.com\n welcome to the tech blog”,
“main method”,
“output”
]| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm - 6of 6 votes
AnswersQ: If you were given a series of equations e.g. [A = B, B = D, C = D, F = G, E = H, H = C]
- aonecoding January 27, 2017 in United States
and then another series [A != C, D != H, ..., F != A ]
Check whether the equations combined is valid.
For the example given, your program should return 'invalid', because the first series implies that A = C, which contradicts the statement A != C in the second series.| Report Duplicate | Flag | PURGE
Amazon Software Engineer Algorithm - 0of 0 votes
AnswersConsidering a server that should ignore requests older than 1 second, create a structure to handle this behavior and give its complexity.
- henriquevalcanaia January 26, 2017 in Brazil for Software Engineering
Use any language you want.| Report Duplicate | Flag | PURGE
Google Intern Algorithm - 0of 0 votes
AnswersImplement, recursively, fast exponentiation and give its complexity.
- henriquevalcanaia January 26, 2017 in Brazil for Software Engineering
Use any language you want.| Report Duplicate | Flag | PURGE
Google Intern Algorithm - -1of 1 vote
AnswersDesign the movement algorithm of a snake from snake game and give its complexity. You can base your idea of algorithm in whatever design for the game. eg. a matrix to represent the grid, use a linked list to represent the snake...
- henriquevalcanaia January 26, 2017 in Brazil for Software Engineering
Use any language you want.| Report Duplicate | Flag | PURGE
Google Intern Algorithm - 0of 0 votes
AnswersCreate a structure to store the median of people ages and give its complexity. If keeping ordered ages, also give the insertion complexity.
- henriquevalcanaia January 26, 2017 in Brazil for Software Engineering
Use any language you want.| Report Duplicate | Flag | PURGE
Google Intern Algorithm - -1of 1 vote
AnswersI was asked the following: Given integers N and A. Find how many integer sequences with elements between 1 and A have sum of all elements equals to N.
- merlinparrajimenez January 26, 2017 in United States
N, A <= 1000.
Sample input: 4 3 , sample output is 7.
In this moment, I realized I do not understand the question. If I have a sequence of 1,2,3, the only sub-sequence that sums 4 is 1,3. So the answer should be 1. What am I missing?
Thank you| Report Duplicate | Flag | PURGE
Algorithm Arrays - 1of 1 vote
AnswersCalculate and replace repeated characters in a string with their number of occurrences.
- krietallo January 24, 2017 in United States for London
Example :
aaaggbbbbc
3a2g4b1c| Report Duplicate | Flag | PURGE
Bloomberg LP Intern Algorithm - 1of 1 vote
AnswersSort elements by frequency, print the elements of an array in the decreasing frequency if 2 numbers have same frequency then print the one which came first.
- krietallo January 24, 2017 in United Kingdom for London| Report Duplicate | Flag | PURGE
Bloomberg LP Intern Algorithm - 1of 1 vote
AnswersWrite a program which will bold the sub-string found in string (HTML Style).
Example: string = "HelloWorld HelloWorld" substringList = ["el", "rl"] Make it like HTML Style:- NewString = "H<b>el</b>loWo<b>rl</b>d H<b>el</b>loWo<b>rl</b>d
But things become more tedious if
- rasmiranjanbabu January 24, 2017 in United StatesExample: string = "HelloWorld HelloWorld AAAAAAABBBBBBBBBBCCCCCCC" substringList = ["el", "rl", "AAAA", "BBBBB", "BC", "BBC"]
| Report Duplicate | Flag | PURGE
Google Software Analyst Algorithm - 1of 1 vote
AnswersGiven an array of integers, design an algorithm that moves all non-zero integers to the end of the array. Minimize the number of writes or swaps.
- pygrammer January 21, 2017 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm Arrays - 0of 0 votes
AnswersHow to verify the string which contains alpha-bates,parenthesis and signglle/double quote
- djvirus January 21, 2017 in India
Ex: AB(CD{"GH"}) is valid
"A()B' is invalid| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 0of 0 votes
AnswersImplement multiple stacks using a single contiguous block of memory
- neer.1304 January 21, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersWrite an efficient solution to give the next best available slot in a parking lot given that you need to minimize the effort to park and exit from the lot.
- neer.1304 January 21, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm - 0of 0 votes
AnswersWrite a program to implement event bus
- neer.1304 January 21, 2017 in United States| Report Duplicate | Flag | PURGE
Ola Cabs SDE-3 Algorithm