## Algorithm Interview Questions

- 1of 1 vote

AnswersImplement a queue using only one stack.

- neer.1304 August 30, 2017 in United States| Report Duplicate | Flag | PURGE

Microsoft SDE-2 Algorithm - 0of 0 votes

AnswersIt’s a two player game. Both the players are equally intelligent to win the game. Give n no. of stones. A player can choose either 1 stone or k stones or l stone (1<k<l). Suppose player 'A' starts game then challenge was to identify the player who will win the game. Player who picks the last 1 stone or last k stone or last l stones win the game.

- neer.1304 August 30, 2017 in United States| Report Duplicate | Flag | PURGE

Microsoft SDE-2 Algorithm - 0of 0 votes

AnswerDesign bus booking system:- Each row has x seat. If customer wants K seats if you have K consecutive seats available, reserve them. Otherwise give seats from any row.

- neer.1304 August 30, 2017 in United States| Report Duplicate | Flag | PURGE

Microsoft SDE-2 Algorithm - 0of 0 votes

AnswersThere is a bridge and N no. people takes (a1,a2,—an) time to cross it and there are K torch and at any time x no of people can pass the bridge and it takes maximum of x people time to cross it. Minimum time required for N persons to cross the bridge.

- neer.1304 August 30, 2017 in United States| Report Duplicate | Flag | PURGE

Microsoft SDE-2 Algorithm - 0of 0 votes

AnswerThere is a graph where each node represents a city and it contains specific no. of people. A tournament is going on and each match is playing in one city. All city’s people gather to watch match. Traffic department wants to manage how many people travel through city x if match is playing in city y for each x. City x and y can be any city.

- neer.1304 August 30, 2017 in United States| Report Duplicate | Flag | PURGE

Microsoft SDE-2 Algorithm - 0of 0 votes

AnswersGiven an Infinite stream of strings as AAAAABBBCCDDDEEE… How will you arrange characters so that string become unique without duplicates .

- neer.1304 August 30, 2017 in United States

Return true if it is possible to arrange else return -1.

Ex . AAABBCCDEF – O/p ABABCDCEF : Possible . AAAAAAAAAAAAAAAAAAAAAAB : Not possible| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - 0of 0 votes

AnswersGiven an array of integers, replace every number with the next higher number to its right. If a number can’t be replaced, we leave it as-it is.

- neer.1304 August 30, 2017 in United States

For example, the list: 5, 2, 1, 4, 6, 7 needs to be changed to 6, 4, 4, 6, 7, 7.| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - 0of 0 votes

AnswersGiven two unsorted arrays A, B. They can contain duplicates. For each element in A count elements less than or equal to it in array B

- neer.1304 August 30, 2017 in United States

Examples:

Input : A = [1, 2, 3, 4, 7, 9]

B = [0, 1, 2, 1, 1, 4]

Output : [4, 5, 5, 6, 6, 6]| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - 0of 0 votes

AnswersYour input is a double array, and you can use *, +, -, and () parenthesis anywhere to create and output the maximum possible value.

- talal.nabulsi5 August 27, 2017 in United States

Ex:

input is {3,4,5,1} --> output: 72

input is {1,1,1,5} --> output: 15

Follow up, if there are numbers <0| Report Duplicate | Flag | PURGE

Google SDE-3 Algorithm - 1of 1 vote

AnswersGiven a continuous stream of numbers, write a logic to find k maximum numbers at any given point of time where k is fixed?

- explorer August 25, 2017 in United States| Report Duplicate | Flag | PURGE

Amazon Software Engineer / Developer Algorithm - 0of 0 votes

AnswerGiven some set of points in each quadrant of a 2D graph and two edges at a fixed angle, find the minimum angle at which the edges would cover maximum points between them?

- explorer August 25, 2017 in United States

I was confused on how to start and interviewer hinted me to consider each point at some angle from base (say 0) and continue finding all points which lies within the fixed angle.| Report Duplicate | Flag | PURGE

Amazon Software Engineer / Developer Algorithm - 0of 0 votes

AnswersGiven a matrix which each element can be the following:

0: not walkable

1: walkable

n: Integer > 1 and is distinct in matrix

Find the minimum number of path it takes to visit each n in ascending order starting from matrix[0][0]. Note matrix[0][0] will always be 1. Allowed moves are (up,right,down,left);

Example:`input: [ [1,1,0,5] [0,1,1,4] ] Output: 5.`

Starting from position (0,0) we need to visit next smallest n which is 4. Min Distance from (0,0) to (1,3) is 4.

- tnutty2k8 August 23, 2017 in United States

Starting from position (1,3) we need to visit next smallest n which is 5. Min Distance from (1,3) to (0,3) is 1.

Total Min Distance is 4 + 1 = 5

Edit:

Allowed moves are [up,right,down,left]| Report Duplicate | Flag | PURGE

Algorithm - 1of 1 vote

AnswersGiven an array of objects with a known set of properties , implement a function that finds all possible partial matches (one object's property value matches the same property on another object), and produce a results object that describes those matches in any format you want.

- ad09 August 23, 2017 in United States| Report Duplicate | Flag | PURGE

Google Intern Algorithm - 2of 2 votes

AnswersGiven an unsorted array of integers, find the length of the longest consecutive elements sequence.

- NoOne August 22, 2017 in India

For example,

Given [100, 4, 200, 1, 3, 2],

The longest consecutive elements sequence is [1, 2, 3, 4].

Return its length: 4.

Your algorithm should run in O(n) complexity.| Report Duplicate | Flag | PURGE

Uber Senior Software Development Engineer Algorithm - 1of 1 vote

AnswersThe stock exchanges work with price matching. A seller comes with a price, and a buyer, given asking for the exact same price are matched, and in quantity.

- NoOne August 22, 2017 in India

Design a system that works.

Considerations:

1. More than a million buy/sale happens in a second.

2. One needs to show a ticker prices - last sold price of a stock.| Report Duplicate | Flag | PURGE

Myntra Software Architect Algorithm - 0of 0 votes

AnswersDesign a Shopping Cart. Come up with anything, how to ensure we scale, and how to ensure discount can be done.

- NoOne August 22, 2017 in India| Report Duplicate | Flag | PURGE

Myntra Software Architect Algorithm - 2of 2 votes

AnswersI was given a questions during an interview which I was not able to solve, please help me in finding the solution.

Ques : - Divide the set in two partition such that both the partition has minimum difference of their sum. If we add an element to the left subset during partitioning than the value of that number will automatically increases by 1, but it will not increase by 1 if I add it to the right side. Find the minimum difference between both the subsets : -`ex :- {1,2,3,4,5} leftSubset = {3,4} , rightSubset = {1,2,5} effective sum of leftSubset = 3+4+2(number of elements) effective sum of rightSubset = 1+2+5 = 8 difference of left and right = (9-8)=1 =, min difference`

solution : (1,2,3} {4,5}

- himanshu.tomar05 August 21, 2017 in India| Report Duplicate | Flag | PURGE

Goldman Sachs Applications Developer Algorithm - 0of 0 votes

AnswersGiven the following input:

- tnutty2k8 August 17, 2017 in United States

Array: array of integer coordinates(x,y) of length N

return a integer M that represents the maximum number of points that falls within the same line.

Example:

Array: [ [0,0], [1,1], [3, 12] ]

Output: 2# [0,0] and [1,1] falls in the same line. [3,12] falls on a different line. Thus the maximum number of points that falls on the same line is 2| Report Duplicate | Flag | PURGE

Algorithm - 0of 0 votes

AnswersGiven the following inputs:

- tnutty2k8 August 17, 2017 in United States

Array: Array of positive non repetitive integers of length N.

K: Integers in range of [2,N)

Target: A Target integer

return any subset of Array with K elements that sums up to target.

Example:

Array: [1,2,3,4,5]

K: 2

T: 6

Output: [1,5]

Array: [1,2,3,4,5]

K: 3

T: 6

Output: [1,2,3]

Array: [1,2,3,4,5]

K: 4,

T: 11

Output: [1,2,3,5]| Report Duplicate | Flag | PURGE

Algorithm - 1of 1 vote

AnswersImplement multithreaded rm -r <folder>, i.e recursively delete files/folder under <folder> by walking through it and assume there can be billions of files under folder and you can only delete folder if all contents in it are first deleted

- Erik August 17, 2017 in United States| Report Duplicate | Flag | PURGE

Google Software Engineer / Developer Algorithm - -1of 1 vote

AnswersGiven a matrix. Convert it into a linked list matrix such that each node is connected to its next right and down node.

Ex:

1 2 3

4 5 6

7 8 9

Output:

1->2->3->NULL

| | |

v v v

4->5->6->NULL

| | |

v v v

7->8->9->NULL

| | |

v v v

--NULL-

This is my code.`class Ideone { public static void main(String args[]) throws Exception { int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; LList op = convert2DArrintoList(arr, 0, 0); System.out.println(op); } public static LList convert2DArrintoList(int arr[][], int col, int row) { if (col >= arr[0].length || row >= arr.length) return null; return new LList(arr[row][col], convert2DArrintoList(arr, col, row + 1), convert2DArrintoList(arr, col + 1, row)); } static class LList { LList(int data) { this.data = data; } LList(int data, LList down, LList next) { this.data = data; this.down = down; this.next = next; } LList() { this.data = Integer.MAX_VALUE; } @Override public String toString() { return " " + this.data + " "; } int data; LList next; LList prev; LList rand; LList down; } }`

Are there better ways of doing it?

- koustav.adorable August 16, 2017 in United States| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - 1of 1 vote

AnswersGenerate all possible matched parenthesis, given n left parenthesis and right parenthesis needs to be matched.

- NoOne August 16, 2017 in India| Report Duplicate | Flag | PURGE

Uber Senior Software Development Engineer Algorithm - 0of 0 votes

AnswersCreate a data structure that stores integers, let then add, delete. It also should be be able to return the minimum diff value of the current integers.

- NoOne August 16, 2017 in India

That is,

min_diff = minimum ( | x_i - x_j | )

Example:

-1,3,4,10,11,11

min_diff = 0

-1,3,4,10,11,14

min_diff = 1| Report Duplicate | Flag | PURGE

Uber Senior Software Development Engineer Algorithm - 0of 0 votes

AnswerSolve the 24 Game

- aonecoding August 14, 2017 in United States| Report Duplicate | Flag | PURGE

Twitter Software Engineer Algorithm - 3of 3 votes

AnswersRound4

- aonecoding August 09, 2017 in United States

Starting from num = 0, add 2^i (where i can be any non-negative integer) to num until num == N. Print all paths of how num turns from 0 to N.

For example if N = 4,

Paths printed are [0,1,2,3,4], [0,1,2,4], [0,1,3,4], [0,2,4], [0,2,3,4], [0,4].

[0,2,4] is made from 0 + 2^1 + 2^1. [0,1,3,4] from 0 + 2^0 + 2^1 + 2^0| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 2of 2 votes

AnswersRound3

- aonecoding August 09, 2017 in United States

For N light bulbs, implement two methods

I. isOn(int i) - find if the ith bulb is on or off.

II. toggle(int start, int end)| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 2of 2 votes

AnswersRound1

- aonecoding August 09, 2017 in United States

Find if two people in a family tree are blood-related.

Round2

Given some nodes in a singly linked list, how many groups of consecutively connected nodes there is.

For linked list

0->1->2->3->4->5->6,

given nodes 1, 3, 5, 6

there are 3 groups [1], [3], and [5, 6].| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - -1of 1 vote

Answerscount number of combinations which are not possible.

- bit32413 August 05, 2017 in United States

There are 'n' empty slots.

A slot can be filled with 'O', 'E', or 'X'

A combination is possible if

1. 'O' s are placed in odd slot , 'E' a are placed in even slots.

2. 'O' and 'E' alternate among them,

i.e (OXOE) not allowed because between O s there is no 'E'; but (OEXXO) is allowed.

some allowed combinations

OEXXX, XXOEO, OXXEX

For 3 slots, not allowed combinations are

OXX

XXO

XEX

XXX

OXO

Only those combinations are considered in which O s and E s are in their respective odd and even slots.

i.e EEXXX will never be considered because a 'E' is in odd slot

A combination isn't allowed if 'O' is not followed by 'E' or vice versa| Report Duplicate | Flag | PURGE

Google SDE-2 Algorithm - 1of 1 vote

AnswersGiven a sorted distinct array of integers and a key K. C closest elements to K are in range [L,R] inclusive, L<=R. Return L as the left index of C closest elements to K.

- anonymous August 04, 2017 in United States

For example:

A = [1, 2, 5, 8, 9, 13]. K = 8 and C = 4. The result L = 3 because 4 closest elements to 8 are [5, 8, 9, 13]| Report Duplicate | Flag | PURGE

Google Intern Algorithm - 0of 0 votes

AnswersGiven a tree print in level zig-zag order.

- tnutty2k8 August 03, 2017 in United States`Example suppose we have the given tree structure: 1 2 3 4 5 6 it should print: 1 3 2 4 5 6 First level prints left to right. Then next level prints right to left. Alternating for each level. Assume the following node structure: Node { data: Integer, left: Node, right: Node, parent: Node, }`

| 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