## Algorithm Interview Questions

- 1of 1 vote

AnswersThere are N countries, each country has Ai players. You need to form teams of size K such that each player in the team is from a different country.

- crowdx February 12, 2019 in India

Given N and number of players from each country and size K. Find the maximum number of teams you can form.| Report Duplicate | Flag | PURGE

Google SDE1 Algorithm - 0of 0 votes

AnswersGiven 2 trees T1 & T2 (both can have > 2 childs), write an algorithm to find if T2 is a subtree of T1.

- sanjos February 09, 2019 in United States

Follow up question, for any branch in T1

a->b->c->d

the following is a valid branch in tree T2(i.e. the isSubTree() algorithm mush evaluate to true in below circumstances)

a->d

a->c->d

c->d| Report Duplicate | Flag | PURGE

Senior Software Development Engineer Algorithm - 1of 1 vote

AnswersYou are given a 2d grid where each grid item has a value of 1 or 0, you can only move horizontally or vertically and if both blocks have value of 1. You are also given a starting index, the output should have the "connected" grid items property to true.

For example:`input = [ [{value: 0}, {value: 1}, {value: 1}], [{value: 0}, {value: 0}, {value: 1}], [{value: 1}, {value: 1}, {value: 1}] ]; startRowIndex = 2; startColumnIndex = 0; output = [ [{value: 0}, {value: 1, connected: true}, {value: 1, connected: true}], [{value: 0}, {value: 0}, {value: 1, connected: true}], [{value: 1, connected: true}, {value: 1, connected: true}, {value: 1, connected: true}] ];`

This is the first part of the question, this can be easily solved using either DFS or BFS.

The second part is you are given the output of the first function and the same start indices. Along with these two input arguments, you are also given a flipIndex. The grid item at the given flip index will have the value flipped. Now give the updated matrix with the updated "connected" path.

- noobtiger February 09, 2019 in United States`input = [ [{value: 0}, {value: 1, connected: true}, {value: 1, connected: true}], [{value: 0}, {value: 0}, {value: 1, connected: true}], [{value: 1, connected: true}, {value: 1, connected: true}, {value: 1, connected: true}] ]; startRowIndex = 2; startColumnIndex = 0; flipRowIndex = 1; flipColumnIndex = 2; output = [ [{value: 0}, {value: 1}, {value: 1}], [{value: 0}, {value: 0}, {value: 0}], [{value: 1, connected: true}, {value: 1, connected: true}, {value: 1, connected: true}] ];`

| Report Duplicate | Flag | PURGE

Software Engineer Algorithm - 0of 0 votes

AnswersGiven a list of sorted lists each of size maximum size M, implement an iterator (maintain the order of items as in the original list of lists).

- sanjos February 04, 2019 in United States

I had a solution requiring extra space using minHeap; However, the interviewer was looking for a constant space solution.| Report Duplicate | Flag | PURGE

Microsoft Software Developer Algorithm - 0of 0 votes

AnswersGiven an array of edges between any two points in 2 dimensional space. A single edge is represented by the co-ordinates of two points it is connecting for example (2,3),(4,5) represents and edge connecting points (2,3) and (4,5).Find out the total number of squares possible if all edges are parallel to X or Y axis.

- jadonv January 26, 2019 in United States

NOTE : Include overlapping squares, squares having one side in common and squares contained within another square. Co-ordinates can have float values.

Example below -

I have considered a very simple input and output combination to keep it short.

Input

{

(0,0),(0,3)

(0,0),(3,0)

(0,3),(3,3)

(3,0),(3,3)

}

Output : 1

Possible Approach : Create a map as below -

Key(Slope of Edge in Degrees) - Value(Array of Edges)

0 - {(0,0),(3,0)},{(0,3),(3,3)}

90 - {(0,0),(0,3)},{(3,0),(3,3)}

While inserting edges in the map, make sure the edges are sorted by max(x1,x2) first and then max(y1,y2).

Pick 2 edges from one slope let's say slope 0, then pick 2 edges from slope 90 and see if square is formed or not. If square not formed, then look at next 2 edges of slope 90 and so on.

Sorting here is an expensive operation.

Please share any better solutions.| Report Duplicate | Flag | PURGE

Amazon SDE1 Algorithm - 0of 0 votes

AnswersYou have a bunch of shops. Each shops sells a bundle of chocolates at a fixed cost. You are given an amount and the price sheet of shops. Find the maximum number of chocolates that you can buy with the amount.

`Int Calculate(Int Amount, []Int Quantities, []Int Cost) { // Implement }`

Other points: (1) You cannot use 'sort' (2) The costliest shop need not sell the maximum number of chocolates. (3) Tricky cases exist. For example: Shop 1 sells 10 chocolates in a 10 $ bundle. Shop 2 sells 9 chocolates in a 1$ bundle. If you have 10$ in your hand, Here the maximum number of chocolates that you can buy is not 10 but 90.

- git January 24, 2019 in United States| Report Duplicate | Flag | PURGE

Numeric Backend Developer Algorithm - 0of 0 votes

AnswersGiven a graph in which there is an edge between (u,v) if gcd(u,v)>g . Given queries of u,v find if a path from u,v exists

- manaranjanfav January 17, 2019 in United States| Report Duplicate | Flag | PURGE

Algorithm - 1of 1 vote

AnswersGiven an integer S, you have to count the total number of integral solutions of the equation a+b^2+c^3+d^4<=S, such that 0<=a,b,c,d<=10000 and 0<S<10^15

- Ankita January 13, 2019 in United States

Edit: Here value can be less than or equal to S, so if input S= 2 ,then output=12

i.e we can consider 0,0,0,1 and 0,0,0,0 etc also as sum will be less than S(i.e 2)| Report Duplicate | Flag | PURGE

SDE1 Algorithm - 0of 0 votes

AnswersGiven two strings Y and Z , return True if y beats z or z beats y .

- saurabh January 10, 2019 in India

Beating Criteria : for i in [1,N] y[i]>=z[i] , if this condition is true for any of permutations of y for any of the permutations of z .| Report Duplicate | Flag | PURGE

Directi Software Engineer Algorithm - 0of 0 votes

AnswerHow to divide a circular array into k group of contiguous element such that difference between maximum sum and minimum sum is minimum. Each group have contiguous element of array. For e.g If the array is as follow. [6 13 10 2] and k=2 then o/p should be 18(6+10+2)-13=5. As array is circular 6,10,2 are contiguous element of array.

- him4211 January 05, 2019 in India

For e.g If the array is as follow. [6 13 2 10] and k=2 then o/p should be 16(6+10)-15(13+2)=1. As array is circular 6,10 are contiguous element of array.

For e.g If the array is as follow. [100 92 133 201 34 34 34 94 108] and k=4 then group as follow 208(108,100), 225(92,133), (201), 196(34,34,34,94) so 225-196=29| Report Duplicate | Flag | PURGE

Samsung Software Developer Algorithm - 0of 0 votes

AnswersGiven a target number and a single number, write a program to find the shortest path to calculate the target number by applying "+-*/" operations to the single number. No parentheses. For example, if we have target number=26 and single number=3 return 3 * 3 * 3 - 3 / 3.

- morfysster January 03, 2019 in United States| Report Duplicate | Flag | PURGE

Algorithm - 4of 4 votes

AnswersGiven a Start Node and an End Node in a graph report if they are “necessarily connected”. This means that all paths from the start node lead to the end node. Report true all paths from start node lead to end node and false if at least one path does not lead to the end node. This is a directed graph which can have cycles

- nikki December 31, 2018 in United States

Does anyone know how to solve this? I had it in my interview at Google in CA and I still cant solve it| Report Duplicate | Flag | PURGE

Google SDE1 Algorithm - 0of 0 votes

Answercode Bubble sort, and modify it to return if the array is already sorted.

- reshma.dhotre November 30, 2018 in India

2.If single swap is needed perform and break without going through o(n2) looping| Report Duplicate | Flag | PURGE

Bloomberg LP Senior Software Development Engineer Algorithm - 0of 0 votes

AnswerIn a hashtable you can only ask for the most recent value for a key. We are going to build a hashtable where you can ask for a key at any time in the past.

- SwampNight November 29, 2018 in United States

API is:

get(k, t) // get the value for k at time t

put(k, v) // insert k, v at time()

time is given as a monotonically increasing version of unix system time.| Report Duplicate | Flag | PURGE

Software Engineer / Developer Algorithm - 0of 0 votes

AnswersGiven a string S of length N. Now, you need to cut the string S into K+1 non-empty substrings by performing K cuts.

- Jatin November 25, 2018 in India for 3

There are lots of ways of performing the cuts in the string S. For every way of performing the cuts, you need to count how many substrings will be a palindrome in that way of cut. You need to sum this count over all possible ways of cutting the string S. Input Format

The first line contains two integers N and K as input. The second line contains the string S as input. Output Format

In the output, you need to print the sum modulo

10^9+7. Constraints

2≤N≤5000

1≤K≤N−1

String S contains only lowercase english alphabets

Sample Input

5 2

aabbc

Sample Output

12

Explanation

In the given test case there are

6 ways to perform the cuts. All the ways are described below.

a | a | bbc = 2 substrings are palindrome

a | ab | bc = 1 substring is palindrome

a | abb | c = 2 substrings are palindrome

aa | b | bc = 2 substrings are palindrome

aa | bb | c = 3 substrings are palindrome

aab | b | c = 2 substrings are palindrome

So, the output is

2+1+2+2+3+2=12| Report Duplicate | Flag | PURGE

Backend Developer Algorithm - 1of 1 vote

Answers`Int minSemsToFinishAllCourses(Map<string, list<string>)`

Given a map containing courses and the list of prerequisites for that course in no particular order, determine the least number of semesters to finish all the courses.

- stevejabs November 22, 2018 in United States

You have to take all prerequisites before you take a given course.

Eg.

Calculus : English, math2

Math2: math 1, Arabic, english

Math1: english

English: <>

Arabic:<>

Give an algorithm for the above and code using java| Report Duplicate | Flag | PURGE

Algorithm Java - 0of 0 votes

AnswerRoulette -Gamblers Fallacy. start with $50, bet opposite color every time same color 4 in a row. loop 100 time or until $0. Suggest create roulette wheel object with history, a gambler object with maybe gamblingplan object. (you can find more detailed suggestions elsewhere)

- whoknows November 18, 2018 in United States| Report Duplicate | Flag | PURGE

Google SDE1 Algorithm - 1of 1 vote

AnswersAdd two numbers represented as LinkedList (not LeetCode 445 which uses ListNode)

- KelvinLong8897 November 17, 2018 in United States

e.g

inputs: '5'->'6'->'3'

'8'->'4'->'2'

output: '1'->'4'->'0'->'5'

method signature:

LinkedList<Integer> sumList(LinkedList<Integer> l1, LinkedList<Integer> l2)| Report Duplicate | Flag | PURGE

Facebook Android Engineer Algorithm - 0of 0 votes

AnswersProgram to find the rank student

- Rising star November 05, 2018 in United States| Report Duplicate | Flag | PURGE

unknown freshers Algorithm - 0of 0 votes

Answers1. Input string s. Check if string s is a valid string with valid brackets

- donkeysnore November 05, 2018 in United States

For example:

(({{}})) is a valid s

{[]} is a valid s

[{[}]] is not valid

2. What kind of tests would you conduct to your program to minimize bugs in your program.

3. On the previous example there is only "()", "{}", and "[]" combination of brackets. If other developers want to add a new kind of brackets such as "<>". What kind of changes would change in your previous program.| Report Duplicate | Flag | PURGE

Bloomberg LP Intern Algorithm - 0of 0 votes

AnswersGiven a matrix of 0's and 1's find the smallest number of groups made of 1's, where one group can cover up to two 1's at the same time vertically or horizontally.

- matk100.100 October 31, 2018

01111

11011

00100

The matrix above has 5 of such groups. I've seen similar questions but there the question was about groups of adjacent 1's. Here the groups are limited.

Another question how it would change, if the group wasn't limited to two but to given k - number of 1's vertically or horizontally. The time complexity should be the most efficient.

My idea here i to iterate through rows and when we find a 1, check it's bottom and right neighbour. If it has a right but no bottom, a group is made and we skip the right neighbour as it is already in a group. When the checked 1 has a bottom but no right, we make a group of them and we can skip checking the right as well i think.| Report Duplicate | Flag | PURGE

Algorithm - 2of 2 votes

AnswersGiven the root of a binary tree, print the nodes column wise and row wise.

`..............6 ............/....\ ...........9......4 ........../..\......\ .........5....1.....3 ..........\........./ ...........0.......7`

The answer would be 5 9 0 6 1 4 7 3.

- Champaklal October 26, 2018 in United States| Report Duplicate | Flag | PURGE

Facebook Software Developer Algorithm - 1of 1 vote

AnswersGiven multiple tuples in the form of (A,B) where A is the parent and B is the child in a binary tree, find if the input is valid or not. 4 error conditions were provided:

- Ankita October 14, 2018 in United States

1. If a parent has more than 2 children,

2. If duplicate tuples entered,

3. If the tree has a cycle,

4. If more than one root possible.

For violation of multiple validity conditions, print the condition coming first in the above order.

If the input is valid, print the tree in a serial representation. For eg: If input is (A,B), (B,C), (A,D), (C,E) , output: (A(B(C(E)))(D))| Report Duplicate | Flag | PURGE

Starup SDE-2 Algorithm Problem Solving - 0of 0 votes

AnswersSuppose If you are hacker, you have to push data to server and find how much data server can accept using minimal number of times. We don't the size of how much server will accept.

- narsimharao.mothkuri October 14, 2018 in India| Report Duplicate | Flag | PURGE

Accolite software Applications Developer Algorithm - 1of 1 vote

AnswerGiven an array and threshold value. The threshold represents the maximum length of subarrays that may be created. Each subarray created has a cost equal to the maximum integer within the subarray. Partition the entire array into subarrays no longer than the threshold and do it at minimum cost. The subarrays are to be chosen from contiguous elements and the given array must remain in its original order. Write a function to return an integer that denotes the minimum cost to partition the array.

- sticker01 September 23, 2018 in United States

For more clarification, here are the test cases.

1. Array: [1,2], threshold: 1.

we divide the array to {(1),(2)} Total cost is 1+2 = 3

2. Array: [1,5,2], threshold: 2.

we divide the array to {(1),(5),(2)} with total cost 1+5+2 = 8. OR

we divide to {(1,5),(2)} with total cost 5+2 = 7 OR

we divide to {(1),(5,2)} with total cost 1+5 = 6.

Of these minimum total cost is 6. So the ans is 6 with subarrays as {(1),(5,2)}| Report Duplicate | Flag | PURGE

Algorithm - 0of 0 votes

AnswersA company wants to fly in a total of 100 candidates for the interview. The company has two office location, one in NY and other in SF and max capacity at each location is 50 candidates. You are given the cost it incurs to fly in each candidate to NY and SF.

`[500, 300],[540, 600],[550, 600],[300, 50]..so on`

Write an algorithm for the minimum total cost?

- nishug001 September 22, 2018 in United States| Report Duplicate | Flag | PURGE

Bloomberg LP Software Engineer Algorithm - 0of 0 votes

AnswersGiven 3 unsorted arrays A, B and C you need to find all possible combinations such that A[i] + B[j] = B[k] + C[l].

- venkataratnamkumar7777 September 22, 2018 in United States| Report Duplicate | Flag | PURGE

Walmart Labs SDE1 Algorithm Arrays - 1of 1 vote

AnswersLets there are n stores.

- sam September 18, 2018 in United States

A customer order x items. All the stores might not have all the items from the order list. So find the store/combination of stores that can serve the order request such that the set that contain least number of stores is selected so that there are lesser number of shipments.| Report Duplicate | Flag | PURGE

Software Developer Algorithm - 0of 0 votes

AnswersPascal Triangle Hockey Stick Identity?

- Pearl September 16, 2018 in United States| Report Duplicate | Flag | PURGE

Algorithm

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window