Given a Sorted Array which can be rotated find an Element in it in minimum Time Complexity.
eg : Array contents can be [8, 1, 2, 3, 4, 5]. Assume you search 8 in it.
Hi
This was the question asked in interview, null pointer exception is very common why it is not declared as checked exception.I googled but did not get proper answer.
Thanks
One of my friend been asked with a question, Retrieving the max top 100 numbers from one hundred million of numbers, in a recent job interview. Do you have any idea to come up with an efficient way to solve it?
regards!
Input : {5, 13, 6, 5, 13, 7, 8, 6, 5}
Output : {5, 5, 5, 13, 13, 6, 6, 7, 8}
Question is to arrange the numbers in the array in decreasing order of their frequency preserving the order of their occurrence.
If their is a tie, for example, here 13 and 6 then the number occurring first in input array would come first in output array.
I know how to do this in O(n^2). But it seems like there exist a better solution.
I've found this, and there is a link to O(n) answer, but it's written in Haskell and not clear for me.
It would be great to get an answer in c# or similar.
Had the following as an interview question a while ago and choked so bad on basic syntax that I failed to advance (once the adrenalin kicks in, coding goes out the window.)
Given a list of string, return a list of sets of strings that are anagrams of the input set. i.e. "dog","god", "foo" should return {"dog","god"}. Afterward, I created the code on my own as a sanity check and it's been around now fora bit. I'd welcome input on it to see if I missed anything or if I could have done it much more efficiently. Take it as a chance to improve myself and learn other techniques:
void Anagram::doWork(list input, list &output)
{
typedef list SortType;
SortType sortedInput;
// sort each string and pair it with the original
for(list<string>::iterator i = input.begin(); i != input.end(); ++i)
{
string tempString(*i);
std::sort(tempString.begin(), tempString.end());
sortedInput.push_back(make_pair(*i, tempString));
}
// Now step through the new sorted list
for(SortType::iterator i = sortedInput.begin(); i != sortedInput.end();)
{
set<string> newSet;
// Assume (hope) we have a match and pre-add the first.
newSet.insert(i->first);
// Set the secondary iterator one past the outside to prevent
// matching the original
SortType::iterator j = i;
++j;
while(j != sortedInput.end())
{
if(i->second == j->second)
{
// If the string matches, add it to the set and remove it
// so that future searches need not worry about it
newSet.insert(j->first);
j = sortedInput.erase(j);
}
else
{
// else, next element
++j;
}
}
// If size is bigger than our original push, we have a match - save it to the output
if(newSet.size() > 1)
{
output.push_back(newSet);
}
// erase this element and update the iterator
i = sortedInput.erase(i);
}
}
Like many other's I began programming at an early age. I started when I was 11 and I learned C when I was 14 (now 26). While most of what I did were games just to entertain myself I did everything from low level 2D graphics, and binary I/O, to interfacing with free API's, custom file systems, audio, 3D animations, OpenGL, web sites, etc. I worked on a wide variety of things trying to make various games. Because of this experience I have tested out of every college level C/C++ programming course I have ever been offered. In the classes I took, my classmates would need a week to do what I finished in class with an hour or two of work.
I now have my degree now and I have 2 years of experience working full time as a web developer however I would like to get back into C++ and hopefully do simulation programming. Unfortunately I have yet to do C++ as a job, I have only done it for testing out of classes and doing my senior project in college. So most of what I have in C++ is still hobby experience and I don't know how to best convey that so that I don't end up stuck doing something too low level for me.
Right now I see a job offer that requires 2 years of C++ experience, but I have at least 9 (I didn't do C++ everyday for the last 14 years). How do I convey my experience? How much is it truly worth? and How do I get it's full value?
The best thing that I can think of is a demo and a portfolio, however that only comes into play after an interview has been secured. I used a portfolio to land my current job.
All answers and advice are appreciated.
I recently heard this was used as an interview question. I suspect there is a very simple answer; I must be over-thinking it.
Can you write Hello World in C without
using any semi-colons? If so, how?
How does a T9 dictionary work? What is the data structure behind it. If we type '4663' we get 'good' when we press down button we get 'gone' then 'home' etc...
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?
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--\
\ |
----------------
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.
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 ?
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?
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.
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
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 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?
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 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
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.