## Recent Interview Questions

- 0of 0 votes

AnswersYou are given a string S consisting of lowercase English letters denoting different types of candies. A substring of a string S is a string S' that occurs in S. For example, "bam" is a substring of "babammm". Each candy costs 1 unit. You can pick some continuous candies such that you can create a palindrome of length K by using some or all picked candies. Your task is to find the minimum cost to create a palindrome of length K.

- rahul123625 May 06, 2018

Input Format:

First line contains string S.

Next line contains an integer T denoting the number of test cases.

Next T lines contain a single integer K.

Output Format:

For each test case, print minimum cost as mentioned above. If you cannot create a palindrome of length K then, simply print -1.

Constraints:

Constraints:

1≤|S|≤10^5

1≤T≤10

1≤K≤10^5

Sample Input

babammm

2

2

5

Sample Output

2

5

Explanation

Test Case 1: You can pick candies denoted by "mm" and create a palindrome of size 2. So the cost will be 2 units.

Test Case 2: You can pick candies denoted by "babam" and rearrange them, "bamab", to create a palindrome of size 5. So the cost will be 5 units.| Report Duplicate | Flag | PURGE

- 0of 0 votes

Answers/**

* Definition for a binary tree node.

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode(int x) { val = x; }

* }

*/`Find the largest duplicate subtree in a binary tree For example, 1 / \ 2 3 / / \ 4 2 4 / 4 The following are two duplicate subtrees: 2 / 4 and 4`

but the former is the largest, thus return the root of the first subtree

- ajay.raj May 05, 2018 in United States| Report Duplicate | Flag | PURGE

Amazon Software Engineer - 0of 0 votes

AnswersWrite a function that receives string with decimal number (i.e. all characters are decimal digits) and prints the sum of all possible substring-numbers, example:

- leonid.ge May 04, 2018 in UK

sum(“123”) = 123 + 12 + 23 + 1 + 2 + 3 = 167| Report Duplicate | Flag | PURGE

Credit Suisse Software Developer Algorithm - 0of 0 votes

AnswerGiven N number of Strings, generate all combination of these String's characters, these Strings must be N long, and must contain only one number of char from each string.

- sapadt May 04, 2018

example: "abc", "def", "ghi" --> adg, adh, adi, aeg ... cfg| Report Duplicate | Flag | PURGE

Morgan Stanley Software Developer Algorithm - 0of 0 votes

AnswersGiven a target string, an input request replaces the word after the given index a->b such as:

- ajay.raj May 04, 2018 in United States

Target string: "Hello world!"

Input:{s:0, a:Hello b: Goodbye}

Output:"Goodbye world!".

The requirement is that input be given to several words that need to be changed at one time: {{s:0,a:Hello,b:Goodbye},{s:11,a:!,b:?},{s:6 a: World,b:friend}}

And each modified index is based on the original unmodified target string so the end result is

"Goodbye friend?"| Report Duplicate | Flag | PURGE

Amazon Software Engineer - 0of 0 votes

Answersgiven a n-ary tree, find the total distance from this node to any other nodes

- ajay.raj May 04, 2018 in United States

class TreeNode {

int val;

List<TreeNode> children;

TreeNode(int val) {

this.val = val;

children = new ArrayList<>();

}

}

public int findDistance(TreeNode root, TreeNode node) {

}| Report Duplicate | Flag | PURGE

Amazon Software Engineer - 1of 1 vote

AnswersHow many squares are inside a m*n rectangle where some grids in the rectangle were grey and the gray grid could not appear in the small squares.

- ajay.raj May 03, 2018 in United States| Report Duplicate | Flag | PURGE

Amazon SDE1 - 1of 1 vote

AnswersGiven an int array, check if all the numbers can be divided into 5 consecutive numbers.

- ajay.raj May 03, 2018 in United States

Eg: [1,2,2,3,3,4,4,5,5,6] Yes: (1,2,3,4,5) (2,3,4,5,6)

Followup: ask if you know that all the numbers are between 1 and 13,

what's a good optimization.| Report Duplicate | Flag | PURGE

Amazon SDE1 - 1of 1 vote

AnswerYou have two matrixs, how to move the first matrix

(move up, down, left, and right) so that the two matrix in the two matrixs match most.`eg: matrix 1: matrix 2: 0 0 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0`

Then, after taking matrix1 (1 left shift + 1 down),

- ajay.raj May 03, 2018 in United States

the number of 1 in the match with matrix2 is 3| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

Answershow to check if two graphs are structurally identical

- ajay.raj May 03, 2018 in United States

class UndirectedGraphNode {

List<UndirectedGraphNode> neighbors;

UndirectedGraphNode() {

neighbors = new ArrayList<>();

}

}| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

AnswersTable: Customers

- prasad.hybris May 02, 2018 in United States

id | name

1|alice

2|bob

3|carol

Table: Orders

id | customer_id | content

1|1|House

2|2|car

3|2|dog

4|10|cow

please write a query which returns all of the invalid orders (where invalid means their customer_id does not have an associated customer)

4|10|cow| Report Duplicate | Flag | PURGE

Google Technical Support Engineer Database - 1of 1 vote

AnswersSuppose you have a list of tasks which need to be executed. Some of these tasks have dependencies which must be executed before they are. Please provide a method which, when given a list of tasks, will provide a valid ordering in return.

- prasad.hybris May 02, 2018 in United States

Example:

Input: [ A, B, C, D ]

A <- B, C

B <- C, D

D <- C

Return: [ C, D, B, A ]| Report Duplicate | Flag | PURGE

Google Technical Support Engineer Trees and Graphs - 0of 0 votes

AnswersThere are N (N > 20) team, each team will play 'M' (say M =3) league match against every other team. Design various classes, and write the code and algorithm to find the winner.

- CoolGuy May 02, 2018 in India

Note: One match can be played on a single day, as there is just one stadium.

Note: No team should play matches on consecutive days.

Note: Algorithm should come up with Quarter Final, Semi Final, and Final matches.

Follow-up Question: If N is odd or even.

How your design will be modified if there are 'S' no. of stadiums.| Report Duplicate | Flag | PURGE

Adobe SDE-3 - 1of 1 vote

Answersconvert a Sorted array to complete BST

- ajay.raj May 02, 2018 in United States| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

AnswersDesign a Twitter-like topic system so that the most popular topics can be retrieved from the system. A post can have one or more topics and these topics can be shared by multiple posts. (hint: thinking of scalability)

- fz May 01, 2018 in United States| Report Duplicate | Flag | PURGE

Software Engineer Software Design - 0of 0 votes

AnswersHow HP Customer Support Works for Users?

- markwood455 May 01, 2018 in United States for HP Customer Support| Report Duplicate | Flag | PURGE

HP Customer Service Sr. technical support - 0of 0 votes

AnswerGiven six digits, find the earliest valid time that can be displayed on a digital clock (in 24-hour format) using those digits.

- translink604 May 01, 2018

For example, given digits 1, 8, 3, 2, 6, 4 the earliest valid time is "12:36:48". Note that "12 : 34 : 68" is not a valid time.

Write a java method:

class Solution {

public String solution(int A, int B, int C, int D, int E, int F);

}

that, given six integers A, B, C, D, E and F, returns the earliest valid time in "HIT :mm: ss" string format, or "NOT POSSIBLE" if it is not possible to display a valid time using all six integers.

For example, given 1, 8, 3, 2, 6, 4 the function should return "12:36:48".

Given 0, 0, 0, 0, 0, 0, the function should return "00 : 00 : oo". Given 0, 0, 0, 7, 8, 9, the function should return "07:08:09".

Given 2, 4, 5, 9, 5, 9, the function should return "NOT POSSIBLE".

Assume that: • A, B, C, D, E and F are integers within the range [0..9].

Correct answer 1 point

Efficient solution 2 points| Report Duplicate | Flag | PURGE

- 0of 0 votes

AnswerGive a connected graph, no cycle,

- ajay.raj May 01, 2018 in United States

Find the node where the average distance from all other nodes is the smallest| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

AnswerInput: an int array without sorting, then do a binary search on it, and find the number of digits that cannot be found by binary search.

- ajay.raj May 01, 2018 in United States| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

Answers<key = "a"; val = "3"; depend_list = null;/>; <key = "b"; val = "a * a";

- ajay.raj May 01, 2018 in United States

Depend_list = {a};/>; <key = "c"; val = "a * b"; depend_list = {a,b};/>;

Lets output a map<key, value>,a -> 3, b -> 9, c -> 27

Follow-up questions: Make a statistic for all keys and corresponding map entries with the same value.| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

AnswersGiven an array, find an x such that all numbers less than x are equal to themselves. Numbers greater than it are equal to x, and all numbers add up to a target

- ajay.raj May 01, 2018 in United States

For example, [1 3 5 7 9] target=24, answer is8. Because when x=8, the array has only 9>8, so 1+3+5+7+8=24 is the closest to the target.| Report Duplicate | Flag | PURGE

Amazon SDE1 - 0of 0 votes

AnswerGive an N-length array with only 0 and 1 inside

- ajay.raj May 01, 2018 in United States

Requires to find the minimum number of conversions needed to convert the array to 0 before all 1.

The conversion is to change a 0 to 1 or a 1 to 0,

And the number of 0 and 1 in the converted array can be arbitrary.

Just find the minimum conversion step| Report Duplicate | Flag | PURGE

Amazon SDE1 - 1of 1 vote

AnswersAssume courses labeled by their index in an array. Given a list of pairs where the first element represents a prerequisite course required for the second course, derive an ordered list of courses.

- vmayer99 April 30, 2018 in United States| Report Duplicate | Flag | PURGE

Software Engineer Algorithm - 0of 0 votes

AnswersFor a given set of non-negative integers get the number of subsets that add up to a target value k.

- vmayer99 April 30, 2018 in United States| Report Duplicate | Flag | PURGE

Linkedin Software Engineer Algorithm - 0of 0 votes

AnswersFor a given array of integers compute the maximum sum of any range in the array. Then modify the function to find maximum product (note the effect of negatives on max product).

- vmayer99 April 30, 2018 in United States| Report Duplicate | Flag | PURGE

Linkedin Software Engineer - 0of 0 votes

AnswersWrite a function to compute n^k. (don't forget negative exponents)

- vmayer99 April 30, 2018 in United States| Report Duplicate | Flag | PURGE

Linkedin Software Engineer Algorithm - 1of 1 vote

AnswersPrint (or return) the longest movie title found by successively matching the last and first words in each title, joining to form new titles, given a file containing a list of movie titles.

- gsgy11 April 30, 2018 in United States

For example: 'OF MICE AND MEN' and 'MEN IN BLACK' join to form 'OF MICE AND MEN IN BLACK'.

You could further join 'OF MICE AND MEN IN BLACK' wth 'BLACK MASS' to form 'OF MICE AND MEN IN BLACK MASS'.

The longest title I found (at 143 characters is): WENT TO CONEY ISLAND ON A MISSION FROM GOD BE BACK BY FIVE WIVES THREE SECRETARIES AND ME WITHOUT YOU CANT TAKE IT WITH YOU WERE NEVER LOVELIER| Report Duplicate | Flag | PURGE

Google Software Engineer - 0of 0 votes

AnswersProblem:

- sarunreddy82 April 29, 2018 in United States

Insert + or – sign anywhere between the digits 123456789 in such a way that the expression evaluates to 100. The condition is that the order of the digits must not be changed.

e.g.: 1 + 2 + 3 – 4 + 5 + 6 + 78 + 9 = 100

I have below C solution - Can you please help me to convert to Java. I need this solution in Java.

#include<stdio.h>

#include<conio.h>

int findnumber(int i,int j)

{

int k;

int n;

for(k = 0; k < j; k++)

{

n = i % 3;

i = i / 3;

}

return n;

}

void main()

{

int i, j, k, current, result, operation;

clrscr();

for(i = 0; i < 19683; i++)

{

if(i%3 == 0)

continue;

current = 0;

result = 0;

for(j = 1; j < 10; j++)

{

k = findnumber(i,j);

if(k==0)

{

current = current * 10 + j;

}

else

{

result = result + (operation == 1 ? current : -current);

current = j;

operation = k;

}

}

result = result + (operation == 1 ? current : -current);

if(result == 100)

{

for(j = 1; j < 10; j++)

{

k = findnumber(i,j);

if(k==0)

printf("%d",j);

else

printf("%c%d",k==1?'+':'-',j);

}

printf("\n");

}

}

getch();

}| Report Duplicate | Flag | PURGE

xyz Java - 0of 0 votes

AnswersImagine a horizontal corridor bounded by y = y1 and y = y2 lines on a 2D-plane. There are N sensors with centers (x, y) each of which operates in a range (r) on the plane . So, they cover some circular areas on the plane. See the figure below.

`o | _____o___o__|____________ y = y1 o |OOO __________oo|_____O______ y = y2 O | O _ _ _ _ _ _ | _ _ _ _ _ _ | o O | o O O | | O | |`

The question is whether a path exist from x=-inf to x=+inf via corridor without being detected any radar.

Constraints:

1. You are free to move any direction only if you stay in the corridor.

2. You are free to go through corridor borders.

3. N sensors are given as list of (x, y, r) like [(1, 3, 2), (-1, -3, 4)]. x and y are signed integers. r is a unsigned integer.

4. y1 and y2 are integers.

My solution:

1. Create an intersection graph of N sensors by comparing them each other via Euclidean distance`(x1 - x2)^2 + (y1 - y2)^2 <= (r1 + r2)^2`

2. If there is a path between y=y1 and y=y2 through intersected sensors, there do not exist any path from x=-inf to x=+inf. Otherwise, there do exist a path. So, I used BFS to search such a path.

Worst Case Complexity:

1. O(n^2)

2. O(V + E) = O(n + intersection_count)`Total: O(n^2)`

My Best Case Optimization for Intersection Graph:

- engineer06 April 29, 2018 in United States

1. For each sensor, create two events for start and end of circles vertically. y = (y1 - r) and y = (y1 + r)

2. For each sensor, register those two events into an array.

3. Use a line sweep algorithm over 2, which is O(nlogn + intersection_count) and intersection_count may be n^2 at worst case.

I wonder if I should have had a better solution with the worst case < O(n^2).| Report Duplicate | Flag | PURGE

Amazon SDE-2 Algorithm - 0of 0 votes

AnswersNumbers with 4 are considered to be unlucky, floor numbers are skipped with numbers with 4; for a top level n, ask how many layers there are actually; for example n=20, that is 18 levels [Remove 4, 14]

- ajay.raj April 28, 2018 in United States| Report Duplicate | Flag | PURGE

Amazon Java Developer

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

Open Chat in New Window