## Madan

BAN USER- 7of 11 votes

AnswersWe have a list of N nodes with each node pointing to one of the N nodes.

- Madan in United States

It could even be pointing to itself. We call a node ‘good’,

if it satisfies one of the following properties:

* It is the tail node (marked as node 1)

* It is pointing to the tail node (node 1)

* It is pointing to a good node

You can change the pointers of some nodes in order to make them all ‘good’.

You are given the description of the nodes.

You have to find out what is minimum number of nodes that you have to change in order

to make all the nodes good.

Input:

The first line of input contains an integer number N which is the number of nodes.

The next N lines contains N numbers,

all between 1 and N.

The first number, is the number of the node pointed to by Node 1;

the second number is the number of the node pointed to by Node 2;

the third number is the number of the node pointed to by Node 3 and so on.

N is no larger than 1000.

Output:

Print a single integer which is the answer to the problem

Sample Input 1:

5

1

2

3

4

5

Sample output 1:

4

Explanation:

We have to change pointers for four nodes (node #2 to node #5) to point to node #1.

Thus 4 changes are required

Sample input 2:

5

5

5

5

5

5

Sample output 2:

1

Explanation:

We have to just change node #5 to point to node #1 (tail node) which will make node #5 good.

Since all the other nodes point to a good node (node #5), every node becomes a good node.| Report Duplicate | Flag | PURGE

Google SDE1 Algorithm - 0of 0 votes

AnswersYou have a plain with lots of rectangles on it, find out how many of them intersect

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 2of 2 votes

AnswersSort a list of numbers in which each number is at a distance k from its actual position

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 0of 0 votes

AnswersStore a set of sudden-death tournament results in a compact format (eg. a bit array) and a set of predicted match results (also in a bit array). Score the predictions, giving one point per correctly guessed match, without unpacking the bit array into a more convenient format (ie. you have to traverse the tree in-place).

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 0of 0 votes

AnswersImplement a simple regex parser which, given a string and a pattern, returns a boolean indicating whether the input matches the pattern. By simple, we mean that the regex can only contain special character: * (star), . (dot), + (plus). The star means what you'd expect, that there will be zero or more of previous character in that place in the pattern. The dot means any character for that position. The plus means one or more of previous character in that place in the pattern.

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 0of 0 votes

Answers1. find all the combinations of a string in lowercase and uppercase. For example, string "ab" -> "ab", "Ab", "aB", "AB". So, you will have 2^n (n = number of chars in the string) output strings. The goal is for you to test each of these string and see if it match a hidden string.

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 0of 0 votes

Answerint[] first = {3}; // size 1

- Madan in United States

int[] second = new int[3]; // size 3

second[0] = 2;

second[1] = 4; //2,4

Second array has enough space to hold all elements of first and second array, where both the arrays are merged. Now write code to have first array into second.

The following Cracking the coding interview code doesn't work.

public static int[] merge2(int[] first, int[] second){

int lastA = first.length-1; //0

int lastB = second.length-1; //2

int indexMerge = (lastA + lastB);

while(lastA >= 0 && lastB >= 0){

if(first[lastA] > second[lastB]){

second[indexMerge] = first[lastA];

indexMerge--;

lastA--;

}else{

second[indexMerge] = second[lastB];

indexMerge--;

lastB--;

}

}

while(lastA >= 0){

second[indexMerge] = first[lastA];

indexMerge--;

lastA--;

}

return second;

}| Report Duplicate | Flag | PURGE

Google SDE1 - 1of 1 vote

AnswersYou are given a 2-Dimensional array with M rows and N columns. You are initially positioned at (0,0) which is the top-left cell in the array. You are allowed to move either right or downwards. The array is filled with 1's and 0's. A 1 indicates that you can move through that cell, a 0 indicates that you cannot move through the cell. Given a function numberOfPaths which takes in the above 2-D array, return the number of paths from the top-left cell to the bottom-right cell (i.e. (0,0) to (M-1,N-1)).

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 3of 3 votes

AnswersConsider the following array {1,2,3,4,5,2,5,4,4};

In the above array, index 4 could be considered as breaking point where summation of 0 to 4 in the array is equal to summation of 5 to end of array. We need to find the breaking point for the given array. I solved this. But follow up was for this array`{1,0,-1,-1,1};`

. Mathematically the later array's breaking point is 2.

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE1 - 0of 0 votes

AnswersImplement Stack operations using two queues. I wrote some code, later he asked, how many elements could be added to this queue. (My code was like this Queue<Integer> q1 = new Queue<Integer>(); ) What would be the maximum number of elements that this queue would accomodate? My code was in java.

- Madan in United States| Report Duplicate | Flag | PURGE

Microsoft SDE1 Algorithm - -2of 2 votes

AnswersGiven a line length insert white space so text is uniformly displayed within the given length

- Madan in United States| Report Duplicate | Flag | PURGE

Google Software Engineer / Developer - 2of 2 votes

AnswersWrite a function that receives three integer inputs for the lengths of the sides of a triangle and returns one of four values to determine the triangle type (1=scalene, 2=isosceles, 3=equilateral, 4=error). Generate test cases for the function assuming another developer coded the function

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE-2 - 1of 1 vote

AnswersEveryone knows that finding a loop in the single linked list is using runner and follower method. Could you provide mathematical proof of correctness for it and why it works. I said something like induction hypothesis. Someone help me with the correct answer.

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE-2 Algorithm - 0of 2 votes

AnswersHow will you find if a string is a substring of another string in O(n) complexity. For example, "tl" is substring of bottle.

- Madan in United States| Report Duplicate | Flag | PURGE

Google SDE-2 Algorithm

RepAre you searching the best and strong Mantra to remove black magic. Here the best and the most powerful Astromagic ...

Rep**Nancy Nash**, Area Sales Manager at Absolute Softech LtdPrior to my current job I was training soap scum in Africa. Earned praised for my work developing wooden trains ...

RepDo you need dua for controlling husband? Contact Guru ji right now. He provides the best and simple dua to ...

Repour goal is to help individuals companies and organizations of all kinds to communicate with their clients customer and employees ...

RepIsotherm provides the best roof insulation products in Cape Town, South Africa. We offer insulation products for roofs, water pipes ...

Rep**James Gulledge**, Dev Lead at ADP

Get you driving in comfort and class in Atlanta! Prestige Luxury Rentals offer a wide selection of premium vehicles Our ...

RepIf you want to attract someone then astrologer kumar can help you. Astrologer kumar is specialized in offering kamdev vashikaran ...

RepSince 1991, Lakeview Blinds Awnings & Shutters offers stunning interior and exterior blinds, shutters, awnings and security doors & windows to the ...

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

Open Chat in New Window