Algorithm Interview Questions
- 1of 5 votes
AnswersFind sum of n elements after kth smallest element in BST. Tree is very large, you are not allowed to traverse the tree.
- gvikram244 December 06, 2016 in India| Report Duplicate | Flag | PURGE
Amazon SDE1 Algorithm - 1of 3 votes
AnswersGiven a random string S and another string T with unique elements, find the minimum consecutive sub-string of S such that it contains all the elements in T.
- Saghar H November 05, 2015 in United States
example:
S='adobecodebanc'
T='abc'
answer='banc'| Report Duplicate | Flag | PURGE
Facebook Software Developer Algorithm - 1of 3 votes
AnswersFirst greater number in an array. Given a large array of positive
- nim January 24, 2012 in United States
integers, for an arbitrary integer A, we want to know the first integer in
the array which is greater than or equal A . O(logn) solution required
ex [2, 10,5,6,80]
input : 6 output : 10
input :20 output : 80| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 1of 3 votes
AnswersYou are given an array of 1's 2's and 3's. Sort this list so the 1's are first, the 2's come second, and the 3's come third.
- Aasen May 23, 2013 in United States
Ex: Input [1, 3, 3, 2, 1]
Output [1, 1, 2, 3, 3]
But there is a catch!! The algorithm must be one pass, which means no merge/quick sort. Also no extra list allocations are allowed, which means no bucket/radix/counting sorts.
You are only permitted to swap elements.| Report Duplicate | Flag | PURGE
Facebook Software Engineer Intern Algorithm - 1of 3 votes
AnswersGiven 2 binary arrays A and B i.e. containing only 0s and 1s each of size N.
- tryingtosolvemystery August 07, 2013 in India
Find indices i,j such that Sum of elements from i to j in both arrays is equal and j-i (i.e. the length of the set i,j) is the maximum possible value.| Report Duplicate | Flag | PURGE
Amazon SDE-2 Algorithm - 1of 3 votes
AnswersWe know a string is Palindrome if it is the same reading from both sides. Now we define the following string also Palindrome:
- amirtar May 05, 2015 in United States
A man, a plan, a canal, Panama!
Write a code that returns if an string is palindrome and it should return true for above input. (Without directly saying, I should conclude that I have to only consider alphanumerical characters in a string). In addition, we assume the string is very long and we can not keep a copy of this string or even a copy of preprocessed version of this string. Therefore the result should be returned with the first sweep of the string.| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm String Manipulation - 1of 3 votes
AnswersYou are given a 2D Array that contains only 0s and 1s in sorted order. i.e. First Os and then 1s.
- tihor January 24, 2015 in India
Array:
0 0 0 1
1 1 1 1
0 0 1 1
0 1 1 1
You have to figure out the row that contains maximum number of 1s.
e.g. in above case we have row 2 as the answer.| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Algorithm - 1of 3 votes
AnswersOn a given array with N numbers, find subset of size M (exactly M elements) that equal to SUM.
- coredo August 02, 2015 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 1of 3 votes
AnswersGiven an array of both positive and negative numbers, find the contiguous range in the array which gives the maximum product. Give an algorithm which runs in O(N).
- Naveen March 15, 2013 in United States| Report Duplicate | Flag | PURGE
Walmart Labs Software Engineer / Developer Algorithm - 1of 3 votes
AnswersInput - List<String> ["star", "rats", "ice", "cie", "arts"]
print all anagrams in buckets:
["star", "rats", "arts"]
["ice", "cie"]
The signature of unimplemented method is given:public void anagramBuckets(List<String> input);
I was given this question during phone interview.
- sunnyhust2005 October 12, 2013 in United States for Facebook Android| Report Duplicate | Flag | PURGE
Facebook Applications Developer Algorithm - 1of 3 votes
AnswersGiven an array of integer, find the minimum in the sliding window of size 4, in most optimal way.
- duskan March 20, 2014 in United States for Ad
ex [2,1,3,4,6,3,8,9,10,12,56]
Output : [1,1,3,3,3,3,8.....]| Report Duplicate | Flag | PURGE
Yahoo Software Engineer / Developer Algorithm - 1of 3 votes
AnswersGiven a number N, find the smallest even number E such that E > N and digits in N and E are same.
- jerinsebastian.punnamada April 08, 2014 in United States
Print NONE otherwise.
Sample:
Input
N = 34722641
Output
E = 34724126
Input
N = 8234961
Output
E = 8236194 (instead of 8236149)
Java solution| Report Duplicate | Flag | PURGE
Yahoo Applications Developer Algorithm Java - 1of 3 votes
AnswersQuestion: Can you break the given string into words, provided by a given hashmap of frequency of word as <word: n>
- nitinguptaiit July 18, 2019 in United States
Example:
HashMap -> {"abc":3, "ab":2, "abca":1}
String: abcabcabcabca
output: Yes; [ abc, abc, abc , abca ]
Example:
HashMap -> {"abc":3, "ab":2}
String: abcabab
output: No
Example:
HashMap -> {"abc":3, "ab":2, "abca":1}
String: abcx
output: No| Report Duplicate | Flag | PURGE
Facebook Algorithm - 1of 3 votes
Answersdesign and implement a calculater that can calculate expressions like:
- srcnaks February 02, 2015 in -
+ 2 4
* 8 ( + 7 12)
( + 7 ( * 8 12 ) ( * 2 (+ 9 4) 7 ) 3 )
(PS:all items are space delimetered.)
Example answers
+ 2 4 => 2 + 4 = 6
* 8 ( + 7 12) => 8 * ( 7 + 12 ) = 152
( + 7 ( * 8 12 ) ( * 2 (+ 9 4) 7 ) 3 ) => 7+8*12+2*(9+4)*7+3 = 148| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm Data Structures Problem Solving Stacks String Manipulation Trees and Graphs - 1of 3 votes
AnswersN*N matrix is given with input red or black. You can move horizontally, vertically or diagonally. If 3 consecutive same color found, that color will get 1 point. So if 4 red are vertically then point is 2. Find the winner.
- amnesiac February 25, 2014 in United States| Report Duplicate | Flag | PURGE
Epic Systems Algorithm - 1of 3 votes
AnswersThere are N objects kept in a row. The ith object is at position x_i. You want to partition them into K groups. You want to move all objects belonging to the same group to the same position. Objects in two different groups may be placed at the same position. What is the minimum total amount by which you need to move the objects to accomplish this?
- saumya.tyagi6 February 01, 2014 in United States
Input:
The first line contains the number of test cases T. T test cases follow. The first line contains N and K. The next line contains N space seperated integers, denoting the original positions x_i of the objects.
Output:
Output T lines, containing the total minimum amount by which the objects should be moved.
Constraints:
1 <= T <= 1000
1 <= K <= N <= 200
0 <= x_i <= 1000
Sample Input:
3
3 3
1 1 3
3 2
1 2 4
4 2
1 2 5 7
Sample Output:
0
1
3
Explanation:
For the first case, there is no need to move any object.
For the second case, group objects 1 and 2 together by moving the first object to position 2.
For the third case, group objects 1 and 2 together by moving the first object to position 2 and group objects 3 and 4 together by moving object 3 to position 7. Thus the answer is 1 + 2 = 3.| Report Duplicate | Flag | PURGE
Facebook SDE-2 Algorithm - 1of 3 votes
AnswersPosition of Knight is given on a chessboard.
- pavi.8081 April 10, 2013 in United States for STB and MVO
Return me something (adjacency matrix or list or anything) which shows all
the positions the knight can reach upto from a given position.
I must be able to tell, from what is returned, if the position is reachable or not
and if reachable I must be able to trace the path from given position to target position
<<FOLLOW-UP>>
For example if 4 cells are reachable from a cell A, then these 4 cells become children of A.
Then from a cell, say B, out of these 4 cells, you can reach 2 more cells: C and D. Then C and D become children of B.
Likewise program need to return me a DS. I have given a valuable hint with this follow-up. I hope this will help| Report Duplicate | Flag | PURGE
Microsoft SDE-2 Algorithm - 1of 3 votes
AnswersDoes a given file name match a single-star pattern? (can't use regex I assume)
- Guy January 18, 2014 in United States
index.html matches *.html
foo.txt does not match *.html
matches(“index.html”, “*html”) returns true
matches(“foo.txt”, “*html”) returns false
matches(“cat”, “c*t”) returns true| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 1of 3 votes
AnswersGive efficient implementation of the following problem.
- sigkill July 08, 2014 in India
An item consist of different keys say k1, k2, k3. User can insert any number of items in database, search for item using any key, delete it using any key and iterate through all the items in sorted order using any key. Give the most efficient way such that it supports insertion, search based on a key, iteration and deletion.| Report Duplicate | Flag | PURGE
Google Intern Algorithm - 1of 3 votes
AnswersGiven
{ //"Restaurant Types"."[categoryNames]" "American" : "[Burger, French fries, Potato Chips]", "Italian":"[Pizza,Bread Sticks, Potato Chips]" }
Assume this kind of data is given as input and loaded into your choice of Data Structure.
- careercupuser June 25, 2014 in United States for Back-End Developer
Using Category name return the no of resturarnt type. Ex: if i/p is Potato Chips, O/P should be : 2 (American and Italian).
Please mention your Data structure and logic.| Report Duplicate | Flag | PURGE
Yelp Software Engineer / Developer Algorithm - 1of 3 votes
AnswersHow to find medium of 1 billion numbers across N distributed machines efficiently?
- seanren7 August 09, 2013 in United States| Report Duplicate | Flag | PURGE
Google SDE-2 Algorithm - 1of 3 votes
AnswersGiven a matrix, you need to create another matrix such that the value (i,j) is either -1, 0 or 1.
- celeritas February 08, 2014 in United States
1 - if multiplication of all values in ith row and jth column is greater than 0.
-1 - if multiplication of all values in ith row and jth column is less than 0.
0 - if multiplication of all the values in ith row and jth column is 0.
e.g.
1 2 3 1
1 0 -1 2
-1 1 1 1
o/p
-1 0 -1 1
0 0 0 0
1 0 1 -1| Report Duplicate | Flag | PURGE
Amazon Software Engineer Intern Algorithm - 1of 3 votes
AnswersWrite a function in C to create a new BST which is the mirror image of a given tree.
- gjp February 24, 2014 in United States| Report Duplicate | Flag | PURGE
NVIDIA Software Engineer Intern Algorithm C Data Structures - 1of 3 votes
AnswersA message containing letters from A-Z is being encoded to numbers using the following mapping:
- MM September 26, 2017 in United States
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).| Report Duplicate | Flag | PURGE
SDE-2 Algorithm - 1of 3 votes
AnswersFind the shortest path in a maze (from origin to destination). I believe we are supposed to use Dijkstra or BFS. But what I am confused with is that Dijkstra computes the shortest path based on the distance of each edge. But a maze doesn't have weighted edges, and its shortest path should be 'minimum number of cells'. How can we make use of Dijkstra, or BFS?
- Guy January 21, 2014 in United States| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 1of 3 votes
AnswersGiven two input string check if anyone is substring of other.
- Jai December 10, 2014 in United States
example aaaaaabbb, aaaabbb
return true
PS: Don't use any internal string library :)| Report Duplicate | Flag | PURGE
SDE-2 Algorithm - 1of 3 votes
Answers== Question ==
- zhaolixi1991 October 30, 2014 in United States
Given a list of TestResult, where each result contains a test score, a student ID and a date, figure out the students' final scores. A final score is the average of a student's top 5 scores.
Here is a sample of the list of TestResult:
50 JACK 5/14/2013
89 ALICE 3/25/2012
70 BOBBY 12/7/2010
60 JACK 8/9/2013
99 MIKE 9/11/2011
100 JOHN 7/4/2011
38 JACK 1/28/2014
46 JACK 11/15/2012
<... more ...>
struct TestResult {
score,
student_id,
date,
}| Report Duplicate | Flag | PURGE
Amazon Intern Algorithm - 1of 3 votes
AnswersCode to create a file system.... Have classes like directory, file and all
- nr April 21, 2013 in United States for Kindle
please write the full code| Report Duplicate | Flag | PURGE
Amazon SDE1 Algorithm