↳
sub is_anagrams{ my ( $s1, $s2 ) = @_; $s1 =~ s/\s//g; $s2 =~ s/\s//g; return ( join( '', sort split( //, $s1 ) ) eq join( '', sort split( //, $s2 ) ) ? 'yes' : 'no' ); } Minder
↳
In Java : public boolean checkAnagram(String one,String two){ if(one == null || two == null){ return false; } if(one.length() != two.length()){ return false; } one = one.toUpperCase(); two = two.toUpperCase(); List list = new ArrayList(); for(int j = 0;j < one.length();j++){ list.add(String.valueOf(one.charAt(j))); } for(int i = 0;i < two.length();i++){ if(!list.contains(String.valueOf(two.charAt(i)))){ return false; } } return true; } Minder
↳
I managed to write it correctly in about 20 minutes and after two incorrect versions, which was pointed out by the interviewers. Once I did it, I was asked to explain time complexity of my solution. My solution had linear time complexity. Minder
↳
public static int findSubArrayThatSumsTo(int[] A, int S) { int sum = 0; int j = 0; for (int i = 0; i S) { sum-=A[j]; j++; } if (sum == S) { System.out.format("Sum (%d) found between indices %d and %d\n", S, j, i); break; } } return j; // return start-index } Minder
↳
Complexity of above is O(n). From the look of it (given two loops), it might seem that complexity is higher. But actually every element is accessed utmost twice. So, O(2n) = O(n). Minder
↳
The answers with keeping the running sum whilst iterating over the array, and advancing the beginning of the sequence pointer if the running sum is greater than the desired sum, only work for sequences of all positive number, right? For arrays that allow negative numbers, we need something 'smarter' like e.g. calculating the array of partial sum and finding the pair with the desired difference - O(n log n). Counter example - look for 5 in the sequence of {1, 7, -3}. The above solutions would find none, whereas the right answer is {1, 7, -3} Minder
↳
The easier approach will be to think of how many cubes will be there without any face. in 3x3x3, total cubes are (3*3)*(3)=27 and only one innermost cube without a face. So its 27-1=26 cubes on all faces. Similarly, for 3x3X4, there are two innermost cubes. So its 3*3*4-2=34. Minder
↳
Just minus the number of the pieces in the inner cube. So it is 3x3x4-1x1x2 = 34
↳
Math question. Easy if you split it into 3 types of pieces: corners (share 3 faces and should be counted once), borders (share 2 faces) and centers. Minder
↳
Build 2 hashes to count elements on MS1 and MS2. Loop through each element on MS1 (or MS2, smaller one) checking hashes. Minder
↳
Is it possible to use the classes (set_union, set_intersection, etc.) or they want you to implement them manually? Minder
↳
↳
my $m = [1,1,2,3,56,4,7,4,3,2,56,1000]; sub first_uniq { my ( $arr ) = @_; my @seq = (); my $hash = +{}; for ( @$arr ) { push @seq, $_ unless $hash->{$_}; $hash->{$_} += 1; } while ( my $c = shift @seq ) { return $c if $hash->{$c} == 1; } return 'nothing'; } Minder
↳
from collections import OrderedDict l = [1,1,2,3,56,4,7,4,3,2,56,1000] counts = OrderedDict() for i in l: counts[i] = counts.get(i, 0) + 1 for key, c in counts.items(): if c == 1: print(key) break Minder
↳
In Java: int[] arrayOne = {1,1,2,3,56,4,7,4,3,2,56,1000}; Map map = new HashMap(); for(int i=0; i Minder
↳
Change query to something like: select business_id from positions where latitude between ?-1 and ?+1 and longitude between ?-1 and ?+1 and distance(latitude, longitude, ?, ?); Geometrically, above is shortlisting the locations within the square and applying distance formula, nearly a circle. This way indexes on latitude and longitude columns can be used. Minder
↳
I can answer this. Imagine a 2-D (cartesian) plane. If there is a point (x,y) and you are interested to know the points within 1 unit distance, you would draw a circle of 1 unit radius around (x,y) and see which points lie within. That is what distance formula is doing. Now if you draw a square of 2 unit size such that (x,y) is it's center, (1) the square would fully enclose the circle we spoke of above (2) vertices of the square would be (x-1,y-1), (x+1, y-1), (x-1, y+1), (x+1, y+1) So the additional conditions in the WHERE clause would "shortlist" the points within the square and then the distance function would get the points within the circle. Simple! :-) Minder
↳
@Bafniz The question is *not* related to math/geography. You must understand the problem is to optimize a query whose bottleneck is that it'll have to do a FULL TABLE SCAN! Math calculations like cos or sin or anything takes nearly a one millionth the time of doing a FULL TABLE SCAN Minder
↳
Solution with test: Integer[] input = new Integer[] {3, 3, 2, 1, 2, 45, 32, 1, 23, 4}; // some array to test Set uniques = new LinkedHashSet(); for(int i=0; i Minder
↳
I think they want you to write down the algorithm and not use the languages resources, so, I write a modification of counting sort, it is O(n): def remove_dup(array): max = 0 for i in array: if i > max: max = i counting = [] for i in xrange(max): counting.append(0) output = [] for i in array: c = counting[i-1] if c == 0: output.append(i) counting[i-1] += 1 return output Minder
↳
sub unique_in_same_order { my $a = shift; my %uniq; return grep { ++$uniq{$_} == 1 } @$a; } Minder
↳
This is the sort of company where they like you provide more than one answer and also to think both like an Engineer and a Computer Scientist. So your first answer could be If I have to do it in a hurry, sort the list using the built-in sort of the language and take the top of the list. this is O(n log(n)) If it is worth it to do it right, use a heap, that you may also know as a priority queue and you have better performance. Minder
↳
Heap insertion is O(n*log n), how can you get better performance?? That said, "Interview Candidate"'s solution does not match the claimed complexity. I think this can be done with a quick selection algorithm, which provides an average linear solution. Minder
↳
Enoch, It's a heap of size 'k' and we're inserting 'n' elements. The Java implementation of PrioritityQueue.add does it in O(n*log(k)) complexity. Related: http://stackoverflow.com/questions/14310164/complexity-of-priorityqueue-addall Minder
↳
In Perl use strict; use bigint; my ($a, $b) = (0, 1); for (1..12) { print "$a\n"; ($a, $b) = ($b, $a+$b); } Minder
↳
Here is a function: (uses iteration. Not recursion) long fibo(int n) { if(n<=0) { cerr<<"Invalid input"; return -1; } else { long a=0, b=1, c; for(int i=1; i Minder
↳
Solution in Java: Using recursion: for(int i=1; i<=number; i++){ System.out.print(fibonaci(i) +" "); } public static int fibonaci(int num){ if(num==1 || num ==2){ return 1; } return fibonaci(num-1)+fibonaci(num-2); } Using for loop: int fibCount = 10; int[] fib = new int[fibCount]; fib[0] = 1; fib[1] = 1; for(int i=2; i < fibCount; i++){ fib[i] = fib[i-1] + fib[i-2]; } for(int i=0; i< febCount; i++){ System.out.print(feb[i] + " "); } Minder
↳
double check your assignment issues
↳
Thank you for your feedback! We try to work every day on improving our selection process. Your thoughts will definitely help us in creating better recruitment procedures. Minder