## Algorithm Interview Questions

- 0of 0 votes

AnswerGiven two positive integers represented as linked lists, provide the sum of the numbers as a linked list.

- anonymous March 14, 2018 in United States`1->2->3 4->5->6 ----------- 5->7->9 1->2->3 4->5 ----------- 1->6->8 4->5->6 7->8->9 ----------- 1->2->4->5`

| Report Duplicate | Flag | PURGE

Bloomberg LP Senior Software Development Engineer Algorithm - 1of 1 vote

AnswersA bus has to travel from A to B and the distance is d miles. There are many gas stations between A and B.

- akira March 12, 2018 in United States

The bus has initially g gallon of gas in tank. 1 gallon of gas travels 1 mile.

Gas stations have inforamtion of remaining distance from station to destination b and max gas that can be filled from the station.

Find the minimum number of stops for bus without running out of gas ever.

eg: gas = 10 , distance = 20

gasStation[] = {{16,3}, {10, 7}, {14, 11},{11, 5}, {7, 6}}

o/p = 1

If bus stops at the stop{14,11} that is 14 miles away from destination and fills 11 gallon then it can reach destination with 1 gallon spare.

It can also stop as {16,3} and {10,7} but its 2 stops and at destination it runs out of gas.

Similarly {11, 5}, {7, 6} has 2 stops but has 1 gallon spare at destination.| Report Duplicate | Flag | PURGE

Microsoft Algorithm - 4of 4 votes

AnswersFeb On-site Google

- aonecoding March 10, 2018 in United States

DP Problem. Given the length and width of a matrix, get the number of paths from bottom-left to bottom right.

You may only walk into those 3 directions ➡ (right) ↗ (upper-right) ↘ (lower-right) at each point.

Follow-up: optimize 2d DP to 1d DP of linear extra space.

Follow-up: what if some cells are blocked

System Design

Availability test/debug on distributed system. Discussed and drafted about failover, replication, NoSQL etc.

Interviewer seemed to be expecting more but time ran out.| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 0of 0 votes

AnswersGiven a biased coin whose probability for Heads is 0.67 and Tails is 0.33, write an algorithm which will print the Heads and Tails with this probability.

- akisonlyforu March 08, 2018 in United States| Report Duplicate | Flag | PURGE

Algorithm - 0of 0 votes

AnswersAisles contain products. Product is only going to be in one Aisle.

- plasmalightwave March 04, 2018 in United States

Product{

AisleID: string

ProductID: string

Price: float

}

Given Array<Product>, find the N highest price combinations. Combination is 1 product from each aisle. You can choose only one instance of each product.

So if you had two aisles

1: {$5,$4,$2}

and

2: {$6,$1)

And they asked for the 2 highest combos you would give $6,$5 and $6,$4| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - -1of 1 vote

AnswersWe have two sequences A and B consisting of integers, both of length N, and you would like them to be (strictly) increasing, i.e. for each K (0 ≤ K < N − 1), A[K] < A[K + 1] and B[K] < B[K + 1]. Thus, you need to modify the sequences, but the only manipulation you can perform is to swap an arbitrary element in sequence A with the corresponding element in sequence B. That is, both elements to be exchanged must occupy the same index position within each sequence.

- koustav.adorable March 03, 2018 in United States

For example, given A = [5, 3, 7, 7, 10] and B = [1, 6, 6, 9, 9], you can swap elements at positions 1 and 3, obtaining A = [5, 6, 7, 9, 10], B = [1, 3, 6, 7, 9].

Your goal is make both sequences increasing, using the smallest number of swaps.

Write a function:

public int minswaps(int[] A, int[] B);

that, given two zero-indexed arrays A, B of length N, containing integers, returns the minimum number of swapping operations required to make the given arrays increasing. If it is impossible to achieve the goal, return −1.

For example, given:

A[0] = 5 B[0] = 1

A[1] = 3 B[1] = 6

A[2] = 7 B[2] = 6

A[3] = 7 B[3] = 9

A[4] = 10 B[4] = 9

your function should return 2, as explained above.

Given:

A[0] = 5 B[0] = 2

A[1] = -3 B[1] = 6

A[2] = 6 B[2] = -5

A[3] = 4 B[3] = 1

A[4] = 8 B[4] = 0

your function should return −1, since you cannot perform operations that would make the sequences become increasing.

Given:

A[0] = 1 B[0] = -2

A[1] = 5 B[1] = 0

A[2] = 6 B[2] = 2

your function should return 0, since the sequences are already increasing.

Assume that:

N is an integer within the range [2..100,000];

each element of arrays A, B is an integer within the range [−1,000,000,000..1,000,000,000];

A and B have equal lengths.

Complexity O(N)| Report Duplicate | Flag | PURGE

Amazon Data Engineer Algorithm - 0of 0 votes

AnswersA and B are playing a perfect 8-9 game. The rules are pretty simple. At each point, you can either insert an 8 at the end of the previous number or a 9. one 8 and one 9 forms a pair. a 9 can only be inserted if there is an 8 which does not form a pair. Perfect solution is the one which has all its numbers in pair. Find out all the possible perfect outcomes of the game in lexicographic order.

- valencia.lucky February 26, 2018 in India

Input-

Input 1: Max length of number

Output-

Your array must return an array of string s containing all possible outcomes.

Example 1:

Input: 4

Output: {"8899", "8989"}

Explanation: There can be only 2 possible outcomes out of 4 as nine must follow eight.

Example 2:

Input: 6

Output:{"888999","889899","889989","898899","898989"}

Explanation: The possible outcomes are 5.| Report Duplicate | Flag | PURGE

Wipro Technologies Senior Software Development Engineer Algorithm - 0of 0 votes

AnswersAssume there is no software like google maps. you are given a map of world. Suppose you are somewhere in the hyderabad.

- syam February 17, 2018 in India

You will have to figure out all the paths from your location to Hyderabad airport.

I have given DFS approach. But the problem is that by doing DFS, a path can cross boundaries of Hyderabad and go so long away from Hyderabad

airport. DFS will take some much time. How to solve this problem?| Report Duplicate | Flag | PURGE

Accolite software Software Analyst Algorithm - 2of 2 votes

AnswersConvert a string with digits into a literal representation of the number like: 1001 to one thousand one

- aonecoding February 15, 2018 in United States| Report Duplicate | Flag | PURGE

Uber Software Engineer Algorithm - 0of 0 votes

Answers

- getPDat February 14, 2018 in United States`We encode a string, s, by performing the following sequence of actions: Replace each character with its ASCII value representation. Reverse the string. For example, the table below shows the conversion from the string "Go VMWare" to the ASCII string "711113286778797114101": // Character G o V M W a r e // ASCII Value 71 111 32 86 77 87 97 114 101 // // We then reverse the ASCII string to get the encoded string 101411797877682311117. // // For reference, the characters in s are ASCII characters within the range 10 - 126 which include special characters. // // Complete the decode function in the editor below. It has one parameter: // encoded - A reversed ASCII string denoting an encoded string s. // // The function must decode the encoded string and return the list of ways in which s can be decoded. static Collection<String> decode(String encoded) { }`

| Report Duplicate | Flag | PURGE

VMWare Inc Staff Engineer Algorithm - 2of 2 votes

AnswersGiven the list of the numbers. In this list, there are the numbers from the Fibonacci sequence.

- denis.zayats February 13, 2018 in Switzerland

Write the algorithm that retrieves all the numbers which belong to the Fibonacci sequence of numbers.| Report Duplicate | Flag | PURGE

MobiCastle Software Engineer Algorithm - 1of 1 vote

AnswersWrite a function that takes two numbers as strings and returns the result as a string:

- NA February 12, 2018 in UK

mul(l string, r string) : string

Assume the numbers might be too large to be parsed into a variable of any numeric type the language has.| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 0of 0 votes

Answers/**

- Interviewer February 07, 2018 in India for TEZ

*

* Google OnSite Round 3

*

* Data structure for Task Dependency

* A task can start only after all its pre-requisites are done

*

* Code the methods addTask(preRequisiteTask, dependentTask)

* and

* getExecutionSequence()

*

*/| Report Duplicate | Flag | PURGE

Google Senior Software Development Engineer Algorithm - 0of 0 votes

Answers/**

- Interviewer February 07, 2018 in India for TEZ

*

* Google OnSite Round 2

*

* Input is an integer array A

* Return an array B such that B[i] = product of all elements of A except A[i]

*

*/| Report Duplicate | Flag | PURGE

Google Senior Software Development Engineer Algorithm - 0of 0 votes

Answers/**

- Interviewer February 07, 2018 in India for TEZ

*

* Google Telephonic Round 2

*

* Given any uppercase string. Report the starting index at which any valid permutation of

* ABCDEF starts. If not found, then report -1.

* Possible permutations of ABCDEF are ABCDFE, BCDAFE, FEDCAB etc (a total of 6! = 720 permutations)

*

*/| Report Duplicate | Flag | PURGE

Google Senior Software Development Engineer Algorithm - 0of 0 votes

Answers/**

- Interviewer February 07, 2018 in India for TEZ

*

* Google Telephonic Round 3

* Find the sum of all nodes stored in a tree

*

*/| Report Duplicate | Flag | PURGE

Google Senior Software Development Engineer Algorithm - 0of 0 votes

Answers/**

- Interviewer February 07, 2018 in India for TEZ

*

* Google Telephonic Round 3

*

* Convert an integer to base-3 equivalent

*

*/| Report Duplicate | Flag | PURGE

Google Senior Software Development Engineer Algorithm - 1of 1 vote

Answers/**

- Interviewer February 07, 2018 in India for TEZ

*

* Google Telephonic Round 1

*

* Given two strings - S1 and S2.

* Arrange the characters of S1 in same alphabetical order as the characters of S2.

* If a character of S1 is not present in S2 - such characters should come at the end of

* the result string, but make sure to retain the order of such characters

* Case sensitivity is irrelevant

* e.g. S1 = "Google", S2 = "dog"

* Output = "ooggle"

*

* e.g. S1 = "abcdedadf", S2 = "cae"

* Output = "caaebdddf"

*

*/| Report Duplicate | Flag | PURGE

Google Senior Software Development Engineer Algorithm - 0of 0 votes

AnswersMinimum Continuous Subsequence: targetList & availabletTagsList are two lists of string.

- pragramticProgrammer January 31, 2018 in United States

Input:

targetList = {"cat", "dog"};

availableTagsList = { "cat", "test", "dog", "get", "spain", "south" };

Output: [0, 2] //'cat' in position 0; 'dog' in position 2

Input:

targetList = {"east", "in", "south"};

availableTagsList = { "east", "test", "east", "in", "east", "get", "spain", "south" };

Output: [2, 6] //'east' in position 2; 'in' in position 3; 'south' in position 6 (east in position 4 is not outputted as it is coming after 'in')

Input:

targetList = {"east", "in", "south"};

availableTagsList = { "east", "test", "south" };

Output: [0] //'in' not present in availableTagsList

Note: targetList will contain Distinct string objects.| Report Duplicate | Flag | PURGE

Amazon SDE1 Algorithm - -1of 1 vote

AnswersGiven a list of characters, write a function to output a list of length of minimum non overlapping subsequences that can partition the input list.

- akira January 25, 2018 in United States

For example:

Input : [a,b,c]

Output: [1,1,1]

Explanation: There are no repeated characters.

Input : [a,b,c,a]

Output: [4]

Explanation: The 'a' is repeated so one subsequence is between a to last a.

Input : [a,b,c,b,a,e,b,a,d,f,g,d,f,i,f,k,l,m,n,m,l]

Output: [8,7,6]

Explanation: max length from 1st 'a' to last 'a' is 8.

1st 'f' to last is 6 adding d to it = 7

so on| Report Duplicate | Flag | PURGE

Amazon Software Developer Algorithm - 2of 2 votes

AnswerGoogle

- aonecoding January 20, 2018 in United States

1st round

Given a box with N balls in it, each ball having a weight, randomly choose pick one out base on the weight.

Input ball1-> 5kg, ball2 -> 10kg and ball3 -> 35kg,

then Prob(ball1 chosen) = 10%, Prob(ball2) = 20%,

Prob(ball3) = 70% ;

Follow-up:

Select a ball randomly based on weights. Once a ball is chosen, remove it. Next time select from the remaining balls. Go on until there is nothing left in the box.| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 3of 3 votes

AnswersFind whether string S is periodic.

- aonecoding January 20, 2018 in United States

Periodic indicates S = nP.

e.g.

S = "ababab", then n = 3, and P = "ab"

S = "xxxxxx", then n = 1, and P = "x"

S = "aabbaaabba", then n = 2, and P = "aabba"

follow up:

Given string S, find out the P (repetitive pattern) of S.| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 3of 3 votes

AnswersDesign a hit counter which counts the number of hits received in the past 5 minutes.

- aonecoding January 18, 2018 in United States

Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.

Example:

HitCounter counter = new HitCounter();

// hit at timestamp 1.

counter.hit(1);

// hit at timestamp 2.

counter.hit(2);

// hit at timestamp 3.

counter.hit(3);

// get hits at timestamp 4, should return 3.

counter.getHits(4);

// hit at timestamp 300.

counter.hit(300);

// get hits at timestamp 300, should return 4.

counter.getHits(300);

// get hits at timestamp 301, should return 3.

counter.getHits(301);

Follow-up:

Due to latency, several hits arrive roughly at the same time and the order of timestamps is not guaranteed chronological.

Follow up 2:

What if the number of hits per second could be very large? Does your design scale?| Report Duplicate | Flag | PURGE

Dropbox Software Engineer Algorithm - 2of 4 votes

AnswersRound3 Google

- aonecoding January 16, 2018 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 i, int j) - i <= j. Switch state (switch on if it's off, turn off if it's on) of every bulb in range i to j.

All bulbs are off initially.| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 3of 3 votes

Answers/**

- aonecoding January 13, 2018 in United States

* Google

* Given a list of non-negative numbers and a target integer k,

* write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

**/| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 4of 4 votes

AnswersAmazon

- aonecoding January 06, 2018 in United States

Given an ArrayList of Nodes, with each Node having an ID and a parent ID, determine whether the List is given in preorder.| Report Duplicate | Flag | PURGE

Amazon Software Engineer Algorithm - 3of 3 votes

AnswersGoogle

- aonecoding January 05, 2018 in United States

Given an array a[] and an integer k, a[i] means flower at position a[i] will blossom at day i. Find the first day that there are k slots between two blooming flowers.| Report Duplicate | Flag | PURGE

Google Software Engineer Algorithm - 2of 2 votes

AnswerTwitter

- aonecoding January 04, 2018 in United States

Create a simple stack which takes a list of elements.

Each element contains a stack operator (push, pop, inc) and a value to either push/pop or two values, n and m,

which increment the bottom n values by m.

Then print the topmost value of the stack after every operation. If the stack is empty, print "empty"| Report Duplicate | Flag | PURGE

Twitter Software Engineer Algorithm - 1of 1 vote

AnswersPrint words in order which are occurring once in huge document. The RAM size 100MB and file size 10 GB.

- Optimist December 16, 2017 in India

Example: I am good. You are good.

Answer : I am You are

Note : It's not datastructure problem where we can simply make use of LinkedHashMap. The file size is going to be huge.| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - 0of 0 votes

AnswersGiven a List determine if contiguous elements of the List sum to an input number. For example: Array/List [6 5 3 2 1 7], and input number 8. The numbers 5 + 3 = 8. Or suppose an input number 10, the elements of the list 2 + 1 + 7 = 10.

- william.watts December 12, 2017 in United States| Report Duplicate | Flag | PURGE

Facebook Senior Software Development Engineer Algorithm

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

Open Chat in New Window