Questions d'entretiens - Software Engineer | Glassdoor.ca

# Questions d'entretiens - Software Engineer

2,560

Questions d'entretien de Software engineer partagées par les candidats

## Le top des questions d'entretien

Trier: PertinencePopulaires Date

### Cette question a été posée à un(e) Software Engineer chez Facebook :

3 nov. 2012
 implement sqrt without using math libray9 réponsese^((ln(x))/2)I think exp and ln still require a Math library. How about using Newton's method to find the root of f(x) = x^2 - a, where x is the solution (the sought square root) and where a is the number for which you want to find the square root?I would have implemented either Taylor or MacLaurin series, centered at an integer number that is closest to the number that you want to find the square root for, such that the square root of this integer is clean. So if you wanted to find the square root of 8.5, I would centre the series at 9 (sqrt(9) = 3), then compute the series at that point. I'd probably choose between 8 and 10 terms, as that is what is used in any scientific calculator.Afficher plus de réponsesActually, to add to that, I wouldn't be able to include 8 - 10 terms, as that would rely on the square root operation itself.... so I'd have to rely on a linear approximation.This is the way to go. Fast inverse square root as used in Quake III. float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; }For detailed explanation of the algorithm, see http://en.wikipedia.org/wiki/Fast_inverse_square_rootI wouldn't know about this algorithm, but I can think of a (definitely slower than this one) bisection algorithm to find the root of x^2 = number.Often what they are looking for is a programming data structures oriented solution. Such as using binary search to find sq root etc.poop

### Cette question a été posée à un(e) Software Engineer chez Morgan Stanley :

28 févr. 2012
 A few questions on basic command-line syntax in Unix shells: 1. How would you log output and error messages from a command to a file? 2. How would you run the same command on every file in a directory? 3. How would you find the PID of a named process (say if you wanted to kill it)?5 réponses1. command >file 2>&1 2. cd dir; for i in *; do command; done 3. ps | grep processname or ps -C processname#3 I disagree, more like ps aux |awk '\$0 ~ /ProcessName/ && \$0 !~ /awk/ {print \$2}' If you want the PID#3 To find the PID: pgrep -xAfficher plus de réponses#3 - to find the PID Or simply use: pidofOpps!! there is typo; it should be: pidof

12 sept. 2012

### Cette question a été posée à un(e) Software Engineer chez McKesson :

6 juin 2011
 Given an array of 1001 elements, consists all numbers from 1-1000. Only one number is repeated. Write a function that returns the repeated number.4 réponsesHonestly, there's two possible answers you could give, depending on whether you want space efficiency or time efficiency (assuming that the array is *not* sorted). For time efficiency, ironically, the brute-force method is fastest. You make a counter array of size 1000, all indices initialized to 0, which takes O(n) time. Then, you run the following: for (int i = 0; i 1) // pre-increment the counter, then check if we've counted 2. return array[i]; } Overall, the time-efficient algorithm takes O(2n) = O(n) time, but takes O(n) additional space. The space-efficient algorithm just quick- or heap-sorts the array in place, then runs a binary search, comparing the value to the index. If the value == array[mid], search higher; otherwise search lower. Leave the loop once (high - low == 1), and then return that value. This will take O(n log n) time due to the sorting process, but only take up O(1) extra space. Of course, if the array comes pre-sorted, you would trivially just run binary search as described above.hey, i have one quick solution, create a function that will take up an initial value and end value, that will add up all numbers between them. create another function to add all elements in the array. call the first function, with value (1, 1000) and call the second function. compare the value. whatever the difference is, that is the repeated number.I think Lucas has the right idea with his counter but there's one flaw to his posted solution. His check should be (if ++counter[array[i]-1] > 1). If you do indeed declare the count tracker as int counter (which is the values counter to counter), doing counter[array[i]] would cause an out of bounds problem for array[i] = 1000. A third alternative, along the lines of dsutandi's idea, is to recognize (if you took a number series class) that the sum of 1 to n = n *(n+1) / 2. So you can loop through the incoming array, sum up all the values, then subtract that sum against the afforementioned value (inserting 1000 for n) and that would be your duplicate. For example: int sum = 0; for (int i = 0; i < 1001; i++){ sum += array[i]; } return sum - (1000*1001)/2;Afficher plus de réponseshere is my take : use following formula to get ideal sum : ideal_sum= n*(n+1)/2 real_sum=summation_of_all numbers in array repeated number = abs(real_sum-ideal_sum)

### Cette question a été posée à un(e) Senior Software Engineer chez Amazon :

16 août 2012
 Write a function that divides one number by another, without using the division operator, and make it better than O(n).7 réponsesThis can be done in a recursive function, the following code is in Python. # get result of a/b without using a "divide" operator def div(a,b): if a < b: return 0 else: return div(a-b, b)+1 This is how human being do the division naturally, however, the running time of this is O(n/m), where n is the size of a, and m is the size of b, which means, O(n/m) is guaranteed to be less than O(n), when m is larger than 1. -MaximThe answer above is still O(n). We can use binary search and find the answer in the interval [1,a] and use multiplication operator.Totally agree with Vasil. Other option: Long Division Algorithm. O(log n) anyway.Afficher plus de réponseswhy not just a * b^(-1) :-)// Write a function that divides one number by another, without using the division operator, // Assume that x%y = 0 // O(log n) (function() { 'use strict'; var divide = function(x, y) { var xLength = (x + '').length; var i = 0; var result = 0; var xAry = ('' + x).split(); var xStart = ''; for (i = 1; i = y) { xStart -= y; result = parseInt(result, 10) + 1; } } } return result; }; console.log(divide(1000, 4)); })();Use logarithms? O(1) log(x/y) = log(x) - log(y) = log(answer) answer = 10 ^ log(answer)Convert the number to divide into the base of the number that you are dividing with and then shift the 'bits' to the right by 1 then convert back to decimal

### Cette question a été posée à un(e) Senior Software Engineer chez TripAdvisor :

2 déc. 2013
 After asking the details of my current role, he only gave me a simple coding question. Write a function using C++ or Java that is passed an integer and it returns the number of bits set to 1. Is there a way to improve your solution and make it faster and more efficient?3 réponsesThere are obviously multiple solutions: Solution 1: Set sum = 0 Find the remainder by dividing by 2. Divide by 2 for the next iteration. Solution 2--much better. Set sum = 0 Start loop Set mask = 1 sum += mask & number Loop return sumThere is another way, that is explained here: http://en.wikipedia.org/wiki/Hamming_weigh (with just 24 operation and without any cycle you can find the number of bit set to 1)An example in Java with 10000 results in answer 5 int number = 10000; int numberOfBitsSet = 0; for (int i = 1; i <= number;) { int result = (i & number); if (result != 0) { numberOfBitsSet++; } i = i << 1; } System.out.println(numberOfBitsSet);

### Cette question a été posée à un(e) Associate Software Engineer chez Electronic Arts :

9 avr. 2009
 Given 100 white marbles and 100 black marbles and two jaws. Put these marbles in the two jars in a way that would maximize the chance of retrieving a white marble from any given jaw.4 réponsesdivide black marbles into the two jars first. then divide the white marbles on top of that in the two jars.Really that is what you would answer? What if they stirred the jars around after you distributed the marbles? Personally I think a better answer is to put 1 white marble in the first far and 99 white marbles and 100 black marbles in the second jar. If you choose the jar at random you now have a 74.87% chance of getting a white marble, regardless of the marbles position in the jar.qq is right, that is the optimum combination.Afficher plus de réponses2 colours, 2 jars. White in one, black in the other

### Cette question a été posée à un(e) Software Development Engineer chez Amazon :

10 nov. 2012
 In a collection of strings, output the string that occurs most often.4 réponsesI honestly had no idea how to solve this in a reasonable amount of time.HashMap map = new HashMap(); for(String str : strings) { Integer count = map.get(str) map.put(str, count != null ? count + 1: new Integer(1) ); } // then iterate through map to find the largest integer, the key will be the string with the most occurancesIts a similar answer to above, but I think using just a hash table is not correct if there is collision. Perfect hashing can be used for no collision, or an array in the hashmap.Afficher plus de réponses@baldeagle you don't need to iterate through the list again to get the max. Just keep track of the curMax as you're inserting into the list.

### Cette question a été posée à un(e) Software Engineer chez Amazon :

31 mars 2013
 How can we store the name of street in a map service? Because street names have characters in common.3 réponsespatricia treeOr a trieTrie 🤗

### Cette question a été posée à un(e) Software Engineer - Customizations chez D2L :

9 oct. 2013
 Given 2 arrays of names, how would you merge them?3 réponsesUse a hash map - most efficientUse a BSTMerge them >> MergeSort
110 de 2,560 Questions d'entretien d'embauche