Stupid Developer
BAN USER- 0of 0 votes
AnswersYou have 100 coins which needs to be distributed among 3 beggars A, B, C. In how many ways you can distribute these 100 coins to all the 3 beggars. Constraint: A cannot have more than 75 coins, B cannot have more than 50 coins, C cannot have more than 25 coins. Write complete code covering all the edge cases. Also suggest test cases.
- Stupid Developer in India| Report Duplicate | Flag | PURGE
Adobe Developer Program Engineer Algorithm
void mirror(struct node* node)
{
if (node==NULL)
return;
else
{
struct node* temp;
/* do the subtrees */
mirror(node->left);
mirror(node->right);
/* swap the pointers in this node */
temp = node->left;
node->left = node->right;
node->right = temp;
}
}
+1. Nice explanation!!
- Stupid Developer November 13, 2013Agree. with uruk
- Stupid Developer November 11, 2013Hey nice explanation about why the strings are immutable. However 4 objects won't be created. Nice explanation by "bo" below.
- Stupid Developer November 11, 2013+1. Correct. Only 2 objects would be created. :)
- Stupid Developer November 11, 2013Do a breadth wise traversal. Break stri1 into 2 parts removing the first and last character. Check for all the char of str2 in both the parts if all the chars present in both the parts. Then continue to divide them into respective parts. Else ignore the part which does not have all the characters. The part u obtain at the depest level will have the minimum window.
For example: str1 : KAMALESH str2: ASH
Break str1 into : KAMALES & AMALESH
Now if use the first part doesn not have H char so ignore that part and break the second part further. into AMALES & MALESH. Continue this to the depeest level where all the char of str2 are present.
Hey even I was thinking of the same solution...but stopped by thinking of the Time complexity for searching the most repeated words node. Every node in the Trie needs to be visited.
- Stupid Developer November 07, 2013Yup, the worst case for all the operation on BST is O(n)
- Stupid Developer November 07, 2013Traverse: O(n). Coz it would be visiting all the nodes once.
Search : O(log n)
Insert : O(log n)
Delete : O(log n)
If there is a frequent access or check for particular words than Trie datastructure should be used.
- Stupid Developer November 05, 2013Yeah thats true...That is the con of Suffix tree....I just suggested 2 ways :)
- Stupid Developer November 02, 2013There are 2 ways.
Either use suffix tree.
Use DFS. Remove last and first character of the strings and create 2 other strings and check for palindrome.Keep doing this for all the substring, until the palindrome is found
Maintain two stacks one for pushing the data while iterating the array and the other while reading and printing the data.
Start from right to left of array
If writing stack is empty push -1. If writing stacks top is greater than current element in the array then push the writing stacks top to the reading stack and current element of array to the writing stack. If it is not greater than starting poping elements from writing stack until the greatest value is found or the stack is empty. If stack is empty and greatest value not found. Push -1 in reading stack and push current element of array in writing stack.
Repeat this for all elements.
private void PrintRightLargest(int[] numbers)
{
Stack<int> writingStack = new Stack<int>();
Stack<int> readingStack = new Stack<int>();
for (int i = numbers.Length - 1; i >= 0; i--)
{
if (writingStack.Count == 0)
{
readingStack.Push(-1);
}
else
{
int stackTop = writingStack.Peek();
if (numbers[i] < stackTop)
{
readingStack.Push(stackTop);
writingStack.Push(numbers[i]);
}
else
{
writingStack.Pop();
if (writingStack.Count == 0)
{
readingStack.Push(-1);
}
else
{
stackTop = writingStack.Peek();
while (writingStack.Count != 0 || stackTop < numbers[i])
{
writingStack.Pop();
if (writingStack.Count > 0)
{
stackTop = writingStack.Peek();
}
}
if (stackTop > numbers[i])
{
readingStack.Push(stackTop);
}
else
{
readingStack.Push(-1);
}
}
}
}
writingStack.Push(numbers[i]);
}
while (readingStack.Count != 0)
{
Console.WriteLine(readingStack.Pop());
}
}
Yeah I agree we cant apply solution 1. We will loose the order.
- Stupid Developer October 29, 2013I can think of 4 solutions as of now:
Solution 1: Time O(nlogn) Space O(1)
Sort the array using quicksort. Traverse the array if the consecutive values are same then remove it and keep shifting the corresponding further values.
Solution 2: Time O(n) Space O(n)
Use hashmap to store the value while traversing the array. If value is already present then remove the element,
Solution3: Time O(n2) Space O(1)
Use two for loops (Brute force method)
Solution 4: Time O(n) Space O(Max value in an array)
Find the largest element in the array ( considering @ can be ignored since its constant for all the values).
Create an array of that size. Iterate the given array and mark the corresponding index in the new array as visited. If value is already marked visited then it is the duplicate.
Are you sure you will go for latter if n would have been huge ??? Just check the time complexity. O(m*n) whereas memory would always be O(n).
- Stupid Developer October 28, 2013There will always be a trade-off between memory and time. The reason i suggested stack is because it looked straight forward and even the interviewer might wanted to test the knowledge of Datastructure. With stack time & memory both are O(n)
- Stupid Developer October 28, 2013Keep pushing the element in stack while reading.
While writing pop from the stack and write it. After every 3rd element take a new line.
Use k-d tree. Kd- Tree solves multi dimensional search problem.
- Stupid Developer October 27, 2013Yeah I just read your below post. Thanks :)
- Stupid Developer October 26, 2013Hey u mentioned that for an n digit number there can be 3N - 1 valid expressions which can be formed. I tried this with a 4 digit number and didn't find it to be working. I found more than 3N - 1 expression that is more than 11 expressions. M i correct or m i missing anything in it. These are the expressions which I found for a 4 digit number 2357
2 + 3 + 5 + 7
2 - 3 - 5 - 7
2 + 3 + 5 - 7
2 + 3 - 5 + 7
2 - 3 + 5 + 7
2 - 3 - 5 + 7
2 + 3 - 5 - 7
23 + 57
23 - 57
2 + 35 + 7
2 - 35 - 7
2 + 35 - 7
2 - 35 + 7
235 + 7
235 - 7
2 + 357
2 - 357
2357
It will fail for the cases (9, 4, 1, 6, 4, 1, 6, 3, 1, 4, 6, 8) & { 8 , 7 , 6 , 5, 1, 1, 2 }
- Stupid Developer October 26, 2013Too many for loops buddy.
- Stupid Developer October 25, 2013Hey I just tooked your code and tried to test it with this input {9, 4, 1, 6, 4, 1, 6, 3, 1, 4, 6, 8}. However it was not giving the correct value. Since am not a PHP developer I might have done some mistake in copying pasting or might be some syntax misunderstanding. Can you just run your code against these values and let me know the result.
Thanks.
private void CalculateStorage(int[] bars)
{
int waterStored = 0;
int maxValue = bars[0];
int possibleWaterCanBeStored = 0;
int lastSuccessfullWaterStoredAt = 1;
bool equalOrGreaterBarEncountered = false;
int lastBarValue = 0;
for (int i = 1; i < bars.Length; i++)
{
if (maxValue > bars[i])
{
possibleWaterCanBeStored += (maxValue - bars[i]);
}
else
{
maxValue = bars[i];
waterStored += possibleWaterCanBeStored;
possibleWaterCanBeStored = 0;
equalOrGreaterBarEncountered = true;
lastSuccessfullWaterStoredAt = i;
}
lastBarValue = bars[i];
}
if (!equalOrGreaterBarEncountered)
{
possibleWaterCanBeStored = 0;
for (int i = lastSuccessfullWaterStoredAt; i < bars.Length; i++)
{
if((lastBarValue - bars[i] ) > 0)
possibleWaterCanBeStored += lastBarValue - bars[i];
}
waterStored += possibleWaterCanBeStored;
}
Console.WriteLine("Water that can be stored is " + waterStored);
}
I agree with jvermette. 20 is the correct answer.
- Stupid Developer October 25, 2013HIRE is valid coz N>= 1.
Also HIRE is valid but HIIREE is invalid.
The question says if W1 and W2 are valid words then W1W2 is also a valid word.
Consider HIRE & HHIIRREE both are valid words. Then HIREHHIIRREE should also be a valid word. Did you missed that scenario or am I making any mistake ?
class Language
{
public void TestForValidLanguage()
{
string languageWord = "HHIIRREEHIREHHHIIIRRREEEE";
bool isValid = IsValidLanguage(languageWord, "HIRE");
}
public bool IsValidLanguage(string languageWords, string language)
{
int counter = 0;
int languageLength = -1;
int languageIndex = 0;
for (int i = 0; i < languageWords.Length; i++)
{
if (languageWords[i] == language[languageIndex]){}
else if (languageWords[i] == language[(languageIndex + 1)%language.Length])
{
if (languageLength == -1)
{
languageLength = counter;
}
if (counter != languageLength)
{
return false;
}
if ((languageIndex + 1) >= language.Length)
{
if (languageLength != counter)
{
return false;
}
//Reset the language length for the new word.
languageLength = -1;
}
languageIndex = (languageIndex + 1)%language.Length;
counter = 0;
}
else
{
return false;
}
counter++;
}
return counter == languageLength;
}
}
Are you asking for a question or are you posting a question :P . However what I can understand from your question is that how to hide a base class function. If thats the question, then it is very much specific to the language you are using. For eg. In C# I can hide a base class methods by adding a new keyword to it.
- Stupid Developer October 23, 2013Dumbo change your name.. u r not one :P . Till now it looks good but not very much sure :)
- Stupid Developer July 08, 2013Constraints wont be maintained. With this you also will get output as 75 + 50 + 25 and so on and so forth
- Stupid Developer July 08, 2013Code Please ???
- Stupid Developer July 08, 2013Code Please ??
- Stupid Developer July 08, 2013Hey first thing which I wanna tell you is that there can b atmost 3 elements. Secondly you are not supposed to use additional memory. :)
- Stupid Developer December 09, 2012Cool...I just forgot that there was even other formula for this...Mathematics rocks :0
- Stupid Developer May 24, 2012
Hey in both the if check...you are performing the same operation...M i missing out something ?
- Stupid Developer November 21, 2013