Software engineer Interview Questions | Glassdoor.ca

# Software engineer Interview Questions

10,595

software engineer interview questions shared by candidates

## Top Interview Questions

Sort: RelevancePopular Date

Nov. 3, 2012
 implement sqrt without using math libray9 Answerse^((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.Show more responsesActually, 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

Sep. 12, 2012

### Intermediate Software Developer at The PEER Group was asked...

Mar. 19, 2009
 Your on a farm, and your in a field with horses and you have a fence that you have to repair. But you left your hammer back at the house, what do you do? Remember the fence is broken and you cannot leave it alone otherwise the horses will escape.7 AnswersRide one horse back to the house to get your hammer and use rope to temporarily secure the fence. or Tie one horse to the fence and go back to the house and get your hammer.Take a belt, if you have one, and tie the fence until you go home for the hammer.Why do you need a hammer to fix the fence? Maybe that assumption needs to be questioned. How is this for an assumption? Since its a farm, chances are the fence is made up of 5-6 ft. long wood pieces; which can be maneuvered into poles by hand. So, I would fix the fence and be on my way.Show more responsesGrab a rock and use it as a hammer. IMO a hammer is the easiest tool possible to replace.If there's absolutely no way to repair the fence without the hammer (can't use your shoe), and you have no assistant, bring the horse(s) with you to the house. Or send them into the barn -- pretty much all horses have a shelter for inclement weather, after all. Or scare all the horses to the far end of the pasture, then tie your shirt to the broken fence piece so it waves in the wind and spooks the horses away. Then go get the hammer. But frankly, if the fence is broken, how come the horses didn't escape before you got there to notice it was busted?Why assume the horses will want to leave? Horses are herd animals. Secure the stallion or lead mare, the rest will stay with them. How did it get broken while the horses were there, anyway? Are any of them injured? Do you have a bigger problem than some loose rails? Who left the horses in a pasture with a broken fence? If it was you, how will you CYA? If it was a coworker, how will you handle it? If it was the farmer's pretty daughter... nevermind.Or maybe just call someone to send over the hammer because the question does not say that I do not have a phone.

### Senior Software Engineer at Amazon was asked...

Aug. 16, 2012
 Write a function that divides one number by another, without using the division operator, and make it better than O(n).7 AnswersThis 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.Show more responseswhy 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

### IOS Developer at Booking.com was asked...

Dec. 19, 2013
 They asked a lot of iOS questions, and some general programming questions. The first question they asked was so obscure that I didn't even really understand it. I was probably dead from that point on. Another question was to figure out a way to combine three lists of items where an item would be placed in a destination array if it existed in any two of the source arrays.6 AnswersFor that, I first came up with a triple loop, but finally came over to using a hash table instead.I solved it using 2 for loops with time complexity O(n^2). Combine all three lists and check if each item exist 2 times then add to destination array otherwise ignore@Kiran: You can't combine those three lists as they are not set so the items inside each of one of them could be repeated. For instance if you have few duplicated items in first list and combine them all, you would assume just because they are repeated they should be consider as the result. The rock solid is to compare list a to list b and have the result in a set and then a with c and b with c. The reason I keep the result in a set is to not have duplicate in the result as well. (Question didn't mention anything about this part so just a suggestion) Now you can work on hash table or other data structure to have a better time complexity as the question doesn't say anything about it.Show more responses\$a = [5, 1, 2, 3, 4]; \$b = [5, 6, 7, 3]; \$c = []; \$hash = []; //Hash the first array foreach(\$a as \$v) { \$hash[\$v] = 1; } foreach(\$b as \$v) { if (\$hash[\$v]) { \$c[] = \$v; } } print_r(\$c);public static void main(String[] args) { int a[] = {1, 2, 1, 4, 6, 7}; int b[] = {7, 6, 3}; int c[] = {5, 3, 4, 0, 9}; int maxLength = Math.max(Math.max(a.length, b.length), c.length); LinkedHashMap freq = new LinkedHashMap(); HashSet aSet = new HashSet(); HashSet bSet = new HashSet(); HashSet cSet = new HashSet(); for (int i = 0; i iterator = set.iterator(); while (iterator.hasNext()) { int key = iterator.next(); if (freq.get(key) >= 2) { System.out.print(key + ","); } } }I guess you were asked to find common elements within three arrays. I would give it a try to use NSCountedSet to count them then populate the result array with those common elements. NSArray *a = @[@1,@3,@8,@-9]; NSArray *b = @[@-1,@3,@0,@-9]; NSArray *c = @[@0,@31,@32,@8,@6]; -(NSArray*)find2ElementsAtleastPresentIn2Arrays:(NSArray*)aList:(NSArray*)bList:(NSArray*)cList{ NSMutableArray *resultArr = [[NSMutableArray alloc]init]; NSMutableArray *largeArray = [[NSMutableArray alloc]init]; [largeArray addObjectsFromArray:aList]; [largeArray addObjectsFromArray:bList]; [largeArray addObjectsFromArray:cList]; NSCountedSet *cs = [[NSCountedSet alloc]initWithArray:largeArray]; for(NSNumber *num in cs){ //NSLog(@"%d **** %@",[cs countForObject:num], num); if([cs countForObject:num]>1){ if(![resultArr containsObject:num]){ [resultArr addObject:num]; } } } NSLog(@"%@", resultArr); return resultArr; }

### Software Engineer at Morgan Stanley was asked...

Feb. 28, 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)?6 Answers1. 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 -xShow more responses#3 - to find the PID Or simply use: pidofOpps!! there is typo; it should be: pidof1. ./sample.sh &> output.txt :- to capture output and error message suppose sample.sh have below script: #!/bin/bash cat file1 cp file2 file3 lets assume there's no file2 exists in the current directory, returns an error message 2. from any current directory run the below line by line. for i in `find -maxdepth 1 -type f` do cat \$i >> output.txt done in for loop i stores all the files (type f means normal file type) in a current directory (maxdepth 1), suppose you want to consider all subdirectory files remove this part. then redirecting all the files to output.txt 3. ps -ef | grep -i "*process name* ps displays running processes, -e utility here displays all the processing shell processes without ignoring any process, f gives the full text/all info of processes. if you are not sure on the exact process name give * as above by giving part of the process name that you can remember

Jan. 27, 2014

### Software Engineer at Zenefits was asked...

Mar. 21, 2015
 Given a Binary Search Tree with integers at every node and an integer k, write code that decides whether or not there exists two nodes a and b such that a+b=k6 AnswersSomething like this? (Untested) private class Node { public boolean ContainsSum(int k) { return ContainsSum_(k, new ArrayList()); } protected boolean ContainsSum_(int k, ArrayList vals) { for (int i : vals) { if (i + m_val == k) return true; } vals.add(m_val); return (m_right != null && m_right.ContainsSum_(k, vals)) || (m_left != null && m_left.ContainsSum_(k, vals)); } private int m_val; private Node m_left; private Node m_right; }can you please list some more questionsPretty good solution listed aboveShow more responsesThe solution mentioned above is O(n^2), because for every node, it is comparing value with all the nodes in Vals array. We can do better than this because we have BST, which is in sorted order. (For unsorted Binary tree, we should have sorted first in O(nlogn) and then searched O(nlogn). O(nlogn) solution - Traverse the BST with DFS (O(n)). For each node of value 'a' search for node of value "b = k - a". Searching in BST would be O(logn). Thus the solution becomes O(nlogn)HashMap tracker = new HashMap(); public SumPair parseTree(Node node, int sum) { if (node == null) { return null; } SumPair pair = parseTree(node.left, sum); if (pair == null) { pair = parseTree(node.right, sum); } if (pair == null) { if (tracker.containsKey(node.data)) { pair = new SumPair(node, tracker.get(node.data)); } else { tracker.put(sum - node.data, node); } } return pair; }With a node reference to left,right and parent, it can be done in O(n) Start at deep left with 2 node references at lowest node, value L. move one reference in DFS way until it find a node R >= K-L. if equal then we have a L and R then move the first reference to the next value in DFS, until node L > K-R repeat until last node is reached and L + R > K lot of micro-optimization possible (skip a whole node by going direct to parent when the parent is still lower than what we looking for)

### Front End Developer at IBM was asked...

Apr. 10, 2019
 1. To add an HTTP request to the search engine, then log a result depending on the# of pages and requirements. 2. Use DP to implement a Knapsack-like problem. Restrictions on time complexity. 3. Implement a to-do list's delete method on website.5 AnswersDid u get rejected or just not receive an offer yet?To X: Get rejectedDid they request to do a background screening on you before rejecting?Show more responsesNoThanks for replying, it seems they requested to do a background check on me currently

### Software Developer at PokerStars was asked...

Jun. 13, 2014
 Very very hard, they ask you to do a poker project for 8 hours and give no feedback after and ask you to leave5 AnswersWell...I did the project and got onsite 1 day later. Got offer right after the onsite.....it got to be your code really really not good.....80k salary +20k bonusBtw I declined the 100k offerSo it's not 100k + 20k bonus :) Well, to go for full day interview several times they have to attract you with something. I bet you feel great... It's definitely cheating... Honestly, I wouldn't waste my time with such interviews even if they promise you 150K + bonus. It's unprofessional :)Show more responsesI got the job and love it. Again,maybe you did not do as well as you thought.Code test was really fun. I enjoyed it, as it makes you think hard. The interview was real grilling. I managed to answer half of the questions. Got the job. Now I am the one grilling people on interviews. If you are weak - go work for a bank or something.
110 of 10,595 Interview Questions