Two BSTs (Binary Search Trees) are given. How to find largest common sub-tree in the given two binary trees?
EDIT 1:
Here is what I have thought:
Let, r1 = current node of 1st tree
r2 = current node of 2nd tree
There are some of the cases I think we need to consider:
Case 1 : r1.data < r2.data
2 subproblems to solve:
first, check r1 and r2.left
second, check r1.right and r2
Case 2 : r1.data > r2.data
2 subproblems to solve:
- first, check r1.left and r2
- second, check r1 and r2.right
Case 3 : r1.data == r2.data
Again, 2 cases to consider here:
(a) current node is part of largest common BST
compute common subtree size rooted at r1 and r2
(b)current node is NOT part of largest common BST
2 subproblems to solve:
first, solve r1.left and r2.left
second, solve r1.right and r2.right
I can think of the cases we need to check, but I am not able to code it, as of now. And it is NOT a homework problem. Does it look like?
We are given a string of the form: RBBR, where R - red and B - blue.
We need to find the minimum number of swaps required in order to club the colors together. In the above case that answer would be 1 to get RRBB or BBRR.
I feel like an algorithm to sort a partially sorted array would be useful here since a simple sort would give us the number of swaps, but we want the minimum number of swaps.
Any ideas?
This is allegedly a Microsoft interview question according to this.
Given an array of size n, which contains numbers in the range 1 to n, and in which each number is present at least once, but some numbers may be missing, how can I find the missing numbers?
Say you have a linked list structure in Java. It's made up of Nodes:
class Node {
Node next;
// some user data
}
and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.
What's the best way of writing
boolean hasLoop(Node first)
which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time?
Here's a picture of what a list with a loop looks like:
Node->Node->Node->Node->Node->Node--\
\ |
----------------
Hello Friends,
Actually I am working on Pdf by using IReport Software. This software provided two detail section but i want this two detail section with Column Header .then say me how i changes it
reply me on [email protected]
This question was asked at interview.
Say I have a contract.
[ServiceContract]
public interface IMyService
{
[OperationContract]
void methodForClientA();
[OperationContract]
void AnothermethodForClientA();
[OperationContract]
void methodForClientB();
[OperationContract]
void AnothermethodForClientB();
}
When a clientA access the contract it should only see the operation contracts
void methodForClientA(),void AnothermethodForClientA().
Is it possible in WCF ?
US coin values:
.01, .05, .10, .25 (*)
What configuration(s) of US coins can be used to match every value from .01-.99 using the fewest coins possible?
How can you find an answer?
* note: there is actually a .50 coin but it is very rarely seen
This is an interview question I faced recently.
Given an array of 1 and 0, find a way to partition the bits in place so that 0's are grouped together, and 1's are grouped together. It does not matter whether 1's are ahead of 0's or 0's are ahead of 1's.
An example input is 101010101, and output is either 111110000 or 000011111.
Solve the problem in less than linear time.
Make the problem simpler. The input is an integer array, with each element either 1 or 0. Output is the same integer array with integers partitioned well.
To me, this is an easy question if it can be solved in O(N). My approach is to use two pointers, starting from both ends of the array. Increases and decreases each pointer; if it does not point to the correct integer, swap the two.
int * start = array;
int * end = array + length - 1;
while (start < end) {
// Assume 0 always at the end
if (*end == 0) {
--end;
continue;
}
// Assume 1 always at the beginning
if (*start == 1) {
++start;
continue;
}
swap(*start, *end);
}
However, the interview insists there is a sub-linear solution. This makes me thinking hard but still not get an answer.
Can anyone help on this interview question?
Recently I was asked to express the DI in colloquial explanation.
I answered :
1)
I am going to a hotel.I ordered food.The hotel management asks me to clean the plates and
clean the tables.So here i am a client,I am responsible for managing the service (Instantiating,executing,disposing).But DI decouples such tasks so the service consumer no need not worry about controlling the life cycle of the service.
2)
He also asked is there any microsoft API follows DI ?.I answered (This was my guess) In WCF you can create a Proxy using ChannelFactory that controls the life time of your factory.
for item (1) he said only 10% is correct
for item(2) he said that is factory pattern not dependency injection.
Actually what went wrong in my explanation (apart from my bad English) ? What is the real answers for those?
I am waiting for your valuable suggestions.
Consider the following class definition:
class StrToTokens {
StrToTokens(const char* str, const char* delimiters = "\t\r\n"); //constructor
string getNextToken();
void reset();
bool empty();
}
Can someone list some good testcases to test the above class.
A few I could think of are:
empty string, empty delimiters, repeated delimiters, consecutive delimiters, string with only delimiters.
However, the interviewer expected some more(better ones). Can you help out.
Thanks.
How can i do subtraction of integers in C without using either the unary or binary '-' operator?
Or can we do the same for other data types like float/double?
I would like the SO community let me know what does juniors and proficient .NET Developers should know regarding the following subjects, also some code examples or brainteasers like the ones here will help a lot.
System Types
Collection and Generics
Configuration and Installation
Monitoring and Debugging
File I/O
Globalization
Different websites are giving different opinions.
My understanding is this:
To clean up or reclaim the memory that an object occupies, the Garbage collector comes into action. (automatically is invoked???)
The garbage collector then dereferences the object. Sometimes, there is no way for the garbage collector to access the object. Then finalize is invoked to do a final clean up processing after which the garbage collector can be invoked.
is this right?
I have recently come across an interesting question on strings. Suppose you are given following:
Input string1: "this is a test string"
Input string2: "tist"
Output string: "t stri"
So, given above, how can I approach towards finding smallest substring of string1 that contains all the characters from string 2?
An interviewer argued me "Genrics are not completely Genrics",
He provided the example (Parameters int k,int d are not generic)
public static void PrintThis<T>(T a, T b, T c, int k,int d)
{
}
He asked me if i prove still it is generics , i will be allowed to take up the next round.
I did not know what he is expecting from me,and what he really means by showing such example.
Guide me how to smartly face such a strange interview ?.
Thanks in advance.
This is common interview question (according to some interview sites) but I can find no normal answers in Internet - some are wrong and some point to complex theory I expect not looked for in interview (like Bressenham algorithm).
The question is simple:
The circle equation is: x^2 + y^2 =
R^2. Given R, draw 0,0-centered circle
as best as possible without using any
floating point (no trigo, square roots, and so on, only integers)
Hi All,
Recently I attended interview in java, the interviewer asked a question like below:
I have a request which go throgh A,B,C modules and response go back throgh A , in module A I need to talk to database and again in module C I need to talk to database, so in this situation how many connections you will open and where do you close those connections?
My Answer: I said that in module A I will open a connection and I will close it then and there, then control go to module B then module C, in module C again I will open one more connection and i will close it again. then he asked me again another question I want to open one connection per one request processing, how can i do this?
I came across this question: say given two weights 1 and 3, u can weigh 1,2 (by 3-1),3,4 (by 3+1). Now find the minimum number of weights so that you can measure 1 to 1000.
So the answer was 1,3,9,27...
I want to know how do you arrive at such a solution means powers of 3. What is the thought process?
Source: http://classic-puzzles.blogspot.com/search/label/Google%20Interview%20Puzzles
Solution: http://classic-puzzles.blogspot.com/2006/12/solution-to-shopkeeper-problem.html
I was asked this crazy question.
I was out of my wits.
Can a method in base class which is declared as virtual be called using the base class pointer which is pointing to a derived class object?
Is this possible?
You are going on a one-way indirect flight trip that includes billions transfers. You are not stopping twice in the same airport. You have 1 ticket for each part of your trip. Each ticket contains src and dst airport. All the tickets you have are randomly sorted. You forgot the original departure airport (very first src) and your destination (last dst).
Design an algorithm to reconstruct your trip with minimum big-O complexity.