↳
I would first answer with, "First, I would analyze the problem and determine if it didn't make better sense to come to the mountain rather than move the mountain. Assuming that's not feasible..." I think that's a key element they're looking for in an answer. That you can look at a major task and first identify if there isn't a better approach. The next element is to determine how you would go about completing a seemingly impossible or gargantuan task. The specifics of this part of the answer don't matter other than to show that you have an understanding that huge problems need to be broken down into smaller, more manageable tasks using the resources you have. Less
↳
When they ask a quesion like this at MS, they do want an answer. If you tell them that you want to consider alternatives up front, they will wave that off and tell you that, in this hypothetical situation, alternatives were already considered and that moving the mountain is the approach was chosen. They really want you to answer the question. The point of this question is - process. They want to see what process you use to solve problems. It is important to show that you solve the problem not by arranging and re-arranging a series of random thoughts but that you can approach it methodically and that this methodology can be applied to any problem. Do not to to some up with a clever answer that attempts to solve the problem - they will just keep insisting that you tackle the problem. If you don't, you won't pass the interview. So, brush up on your problem solving process before you interview at MS. Use these questions as an opportunity to impress them with how well you can solve difficult problems. Less
↳
This is a common dorky Computer Science joke. The answer I believe they are looking for is that you use the Tower of Hanoi algorithm to move the mountain (i.e. that the problem of moving Mt. Fuji is reducible to the already-solved Tower of Hanoi problem). This could be accomplished by having a large laser and a couple of really good cranes. Less
↳
Put all the numbers from the array into a hash. So, keys will be the number and values of the keys be (sum-key). This will take one pass. O(n). Now, foreach key 'k', with value 'v': if k == v: there is a match and that is your pair. this will take another O(n) pass totale O(2n) ~ O(n) Less
↳
Easiest way to do it. Written in python. If you consider the easiest case, when our summed value (k) is 0, the pairs will look like -50 + 50 -49 + 49 -48 + 48 etc.... etc... So what I do is generalize the situation to be able to shift this k value around. I also allow us to change our minimums and maximums. This solution assumes pairs are commutative, i.e. (2, 3) is the same as (3, 2). Once you have the boundaries that you need to work with, you just march in towards k / 2. This solution runs in O(n) time. def pairs(k, minimum, maximum): if k >= 0: x = maximum y = k - maximum else: x = k + maximum y = minimum while x >= k / 2 and y <= k / 2: print str(x) + " , " + str(y) + " = " + str(x + y) x = x - 1 y = y + 1 Less
↳
here is my solution using hash table that runs in O(2n) => O(n): public static String findNums(int[] array, int sum){ String nums = "test"; Hashtable lookup = new Hashtable(); for(int i = 0; i < array.length; i++){ try{ lookup.put(array[i], i); } catch (NullPointerException e) { System.out.println("Unable to input data in Hashtable: " + e.getMessage()); } } int num2; int num1; for (int i = 0; i < array.length; i++){ num2 = sum - array[i]; Integer index = (Integer)lookup.get(num2); if ((lookup.containsKey(num2)) && (index != i)){ num1 = array[i]; nums = array[i] + ", and " + num2; return nums; } } //System.out.println(lookup.get(-51)); return "No numbers exist"; } Less
↳
All of the previous answers are somehow wrong or misleading. "Not-a-mathematician": the method you describe would ensure that you get 2 DIFFERENT socks instead of matching - and only in the situation that the ratio is exactly 50-50. "Anonymous on Oct 20 2012": No, you could also have 3 of the same sock after grabbing 3. "Anonymous on Oct 3": The probability has little to do here, while it is over 0%. THE REAL ANSWER: Given that there are 2 types, and you want to get a MATCHING PAIR (not 2 different socks) you must grab 3. When you have 3, you WILL have at least 2 of the same kind, since there are only 2 kinds available. Less
↳
1 black : 19 white. .. 3 socks 2 black : 18 white ... 3 socks 3 black : 18 white ... 3 socks 4 black : 16 white.. . 3 socks 5 black : 15 white .. . 3 socks 6 black : 14 white ... 3 socks . .. . .3 socks. why? The worst case scenario is always 2 of one color and one of the other. Less
↳
I'm not a mathematician, statistician or highly analytical but if you pick up 3 socks they could still be all the same type - even if the odds are 50%. Odds do not equal reality. So the only way to "ensure you have a matching pair"is to pick up 11 of the 20. This is the only fool proof guaranteed way to get a pair (in the real world and not the world of odds). Less
↳
first clarify if it is ASCII or UNICODE string For ASCII, create BOOL checkArray [128] = {false}; walk the string and update the index of checkArray based of the character. for (int index=0;index< strlen(str); index++) { if (checkArray[str[index]] == true) { printf (str[index]); return; } else { checkArray[str[index]] = true; } } Less
↳
for (int i=0;i
↳
public static in findDuplicateChar(String s) { if (s == null) return -1; char[] characters = s.toCharArray(); Map charsMap = HashMap(); for ( int index = 0; index < characters.length; index++ ) { // insert the character into the map. // returns null for a new entry // returns the index if it previously if it existed Integer initialOccurence = charsMap.put(characters[index], index); if ( initialOccurence != null) { return initialOccurance; } //there where no characters that where duplicates return -1; } } Less
↳
Iam ready
↳
plz tell me which type of question put up in interview
↳
The algorithm used is: For each number n (ranging from 1 to 10), we have to skip n places in the array. And then put number n at that position. The catch in the program is that we can skip only, not occupied places (the array places where value is 0 by default). Also, when we reach the end of the array, we have to move back to beginning of array (similar to cards being kept at the bottom of the pile of cards.) Here, a variable 'pos' is used to store the final position of any element. Initially, pos is at 0. The skip() takes the array, the current value of pos and the number of skips to be made. For example, for n=1, the values passed to skip() would be: pos=0, n=1. This means that we have to skip 1 place starting from index 0. Similarly, for n=2, pos=1. This means we have to skip 2 places starting from index 1. Java Implementation: public class StackOfCards { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = new int[10]; int pos=0; for(int n=1;n0){ if(arr[pos]==0){ //reduce the number of skips to be made n--; } pos++; //if end of array is reached, take pos back to beginning if(pos==10){ pos=0; } } // keep incrementing pos until a not occupied index is reached. while(arr[pos]!=0){ pos++; } return pos; } } Less
↳
Here is C# Code... namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int n = 36; int[] series = new int[n]; int LargestNumber = n; int SmallestNumber = 1, temp = 1, i; bool putLargeNumber = true; for (i= 0; i 0)) { series[i] = LargestNumber; Console.Write("{0} ", series[i]); LargestNumber = LargestNumber - 1; putLargeNumber = false; temp--; } else { series[i] = SmallestNumber; Console.Write("{0} ", series[i]); SmallestNumber += 1; putLargeNumber = true; temp = SmallestNumber; } } } } } Less
↳
Possible implementation in C++ -------------------------------------------------- int CounterStep(int counter,int N); int LocatePosition(int *cards,int N, int startPointer,int value); using namespace std; void main() { int cards[20]; int N=20; int POS = 0,TOP = 0; // Initializing everything to zero for(int i=0;i Less
↳
Iterate the array and add each number to a set, if number is already there, it won't be added again, thus removing any duplicates. Complexity is Big-O of N Less
↳
The array is already sorted, no need for a set. example: 2,2,5,7,7,8,9 Just keep tracking the current and previous and the index of the last none repeated element when found a difference copy the element to the last none repeated index + one and update current and previous, no extra space and it will run in O(n) Less
↳
public RemoveDuplicates() { int[] ip = { 1, 2, 2, 4, 5, 5, 8, 9, 10, 11, 11, 12 }; int[] op = new int[ip.Length - 1]; int j = 0, i = 0; ; for (i = 1; i <= ip.Length - 1; i++) { if (ip[i - 1] != ip[i]) { op[j] = ip[i - 1]; j++; } } if (ip[ip.Length - 1] != ip[ip.Length - 2]) op[j] = ip[ip.Length - 1]; int xxx = 0; } Less
↳
* Compact a string. i.e remove spaces * public class CompactString { String compactString(String s) { String a = ""; String[] tabStrings = s.split(" "); for (String t : tabStrings) a += t; return a; } } Less
↳
* reverse a string. public class ReverseString { String reverseString(String str) { String rev = ""; for (int i = str.length() -1 ; i >= 0; i--) rev += str.charAt(i); return rev; } } Less
↳
remove all the given characters from a string. public class RemoveChar { String removeChar(String s, char c){ String a = ""; for(int i = 0; i < s.length(); i ++){ if(s.charAt(i) != 'a') a += s.charAt(i); } return a; } } Less
↳
I solved by Sorting as well as using HashMap. I was asked to implement sort which I had never encountered in my past interviews. Less
↳
My version public class PalindromTwoWords { boolean isPalindrome(String a, String b) { boolean isPalindrome = false; for (int i = 0; i 0; j--) { if (a.substring(0, i).equals(b.substring(j))) isPalindrome = true; } } return isPalindrome; } } Less
↳
Sorry when i pasted the code some words escaped. public class PalindromTwoWords { boolean isPalindrome(String a, String b) { boolean isPalindrome = false; for (int i = 0; i 0; j--) { if (a.substring(0, i).equals(b.substring(j))) isPalindrome = true; } } return isPalindrome; } } Less
↳
I think the answer should traverse both link lists and add each node data and make a new link list with digit on each node of the answer. In this way we can traverse in N time and at the end if any nodes are left in second link list we can attach them at the end of new link list Less
↳
//I hope this Solution Would help public ListNode reverse(ListNode head) { ListNode current=head; ListNode previous=null; ListNode next=null; while(current!=null) { next=current.next; current.next=previous; previous=current; current=next; } return previous; } public ListNode addTwoNumbers(ListNode head1,ListNode head2) { head1=reverse(head1); head2=reverse(head2); int sum=0;int carry=0; LinkedList result = new LinkedList(); while(head1!=null || head2!=null ) { sum=((head1==null) ?0 :head1.value )+ ((head2==null) ?0 :head2.value)+carry; //carry=0; if(sum>9) { carry=sum/10; sum=sum%10; } else { carry=0; } result.add(sum); if(head1!=null) { head1=head1.next; } if(head2!=null) { head2=head2.next; } } if(carry!=0) { result.add(carry); } ListNode dummy=reverse(result.head); return dummy; } Less
↳
Is this to remove duplicates from linked list or just combining both the linked lists blindly? Less