# 365 k

Questions d'entretien pour Software Engineer partagées par les candidats

## Principales questions d'entretien

Trier: Pertinence|Populaires|Date
On a demandé à IOS Developer...14 janvier 2016

### Phone interview question # 1: Given an array (for example, [ 1, 0, 2, 0, 0, 3, 4 ]), implement methods that 1. returns the number of non-zero elements (4) 2. moves the non-zero elements to the beginning of the array (the rest of the elements don't matter) -> both [ 1, 2, 3, 4, 0, 0, 0] and [ 4, 1, 3, 2, X, Y, Z ] are valid

15 réponses

let givenArray = [1,0,2,0,0,3,4] var arrayWithNonZeroElements = givenArray.filter({ \$0 != 0 }) let arrayWithZeroElements = givenArray.filter({ \$0 == 0 }) print(arrayWithNonZeroElements.count) //"4\n" arrayWithNonZeroElements.appendContentsOf(arrayWithZeroElements) print(arrayWithNonZeroElementst) // [1,2,3,4,0,0,0] Moins

The answer from OP is good, but there is a case where it doesn't work : if the last value of the array is @0 and the array contains another @0 (Ex: [@1, @0, @2, @0], then you end up swapping two @0 and not solving fully the problem. There is a little optimization possible on the ending condition of the for loop : ending when we reach exchangeObjectAtIndex:indexOfLastNonZeroItem instead. Here is my full solution : - (NSInteger) countAndRearrangeNonZeroElementsFromThisArray: (NSMutableArray*) originalArray { NSInteger countOfZeros = 0; NSInteger indexOfLastNonZeroItem = [originalArray count] - 1; while ([originalArray[indexOfLastNonZeroItem] isEqualToNumber: @0]) { indexOfLastNonZeroItem--; } for(NSInteger index = 0; index &lt; indexOfLastNonZeroItem; index++) { NSNumber * numberFromArray = [originalArray objectAtIndex:index]; if([numberFromArray isEqualToNumber:@0]){ countOfZeros++; [originalArray exchangeObjectAtIndex:index withObjectAtIndex:indexOfLastNonZeroItem]; indexOfLastNonZeroItem--; } } return countOfZeros; } Moins

modified version of the above answer, that works with inout arrays func countZerosAndMoveToBegning(array: inout [String]) -&gt; UInt { let nonZeros = array.filter { \$0 != "0" } let zeros = array.count - nonZeros.count let arrayZeros = Array(repeating: "0", count: zeros) array.removeAll() array.append(contentsOf: arrayZeros) array.append(contentsOf: nonZeros) return UInt(zeros) } Moins

Afficher Plus de réponses

### Given the alphabet encoded as numbers (e.g., a=1, b=2, ..., z=26), and a sequence of numbers (e.g., "23413259802"), how many strings can be generated.

15 réponses

Unlike the other python2.7 solution this one actually works and doesn't waste time with recursion. def find_strings(x): nums = [] prev = "" total = 0 for i in str(x): if int(i) != 0: total += 1 nums.append(int(i)) if prev: if int(prev + i) != int(i) and int(prev+i) &lt; 27: total += 1 nums.append(int(prev+i)) prev = i return total, nums print find_strings(23413259802) Moins

static int foo(String s, int si) { for (int i = si; i &lt; s.length(); i++) { if (i+1 &lt; s.length()) { char c = s.charAt(i); if (c == '1' || (c == '2' &amp;&amp; s.charAt(i+1) &lt; '7')) { int v = foo(s, i+1) + foo(s, i+2); return v; } } } return 1; } Moins

# DFS with extra function to account for dead ends of "00" since no combinations after that def findCombos(s, depth): total = 0 if len(s) &gt; 0 and (int(s) &gt; 0): total += findCombos(s[1:], depth+1) + depth if len(s) &gt; 1 and int(s[0:2]) 0: total += findCombos(s[2:], depth+1) + depth return total def findSpaces(s): total = findCombos(s,1) #in case single character for i in range(1, len(s)): if i &lt; len(s)-1 and s[i:i+2] == "00": total+= findCombos(s[i+2:], 1) return total Moins

Afficher Plus de réponses

### A train leaves San Antonio for Huston at 60mph. Another train leaves Huston for San Antonio at 80mph. Huston and San Antonio are 300 miles apart. If a bird leaves San Antonio at 100mph, and turnes around and flies back once it reaches the Huston train, and continues to fly between the two, how far will it have flown when they collide.

15 réponses

There are several scenarios to consider. Scenario 1 - Since trains rarely collide, the bird will hypothetically continue to fly forever, thus the answer is infinity miles. However, this is obviously not a rational outcome since the bird will tire, need to stop for nourishment, and require rest. In addition, one would need to know more about the bird such as the average lifespan and the realistic length of time the bird can fly at a sustained speed of 100 miles per hour. Of course, the term "bird" is sometimes used colloquially when referring to an airplane. Scenario 2 - Assuming both trains leave at the exact same moment, then the distance between them will be reduced by 140 miles per hour, which is the combined speeds of both trains. Therefore, it will take (300/140) = 2.14 hours before the two trains collide thus the bird would have flow (2.14*100) = 214 miles. However, this assumes both trains are traveling at a constant speed on a single track void of any modern collision avoidance systems, and that neither train makes any scheduled stops along the way, which again is not a real world scenario. Of course, the only bird capable of flying 100 mph sustained for more than 2 hours is an airplane. Scenario 3 - Assuming I am interviewing for an upper level management position and solving the problem will significantly impact the company's bottom-line and thus impact my bonus, I would hire a management consulting company specializing in mathematic modeling to solve the problem and offer suggestions to optimize the collision such that it would conserve as much energy as possible for both trains and the bird. Moins

The distance is infinite as there is no relaxation time or turning time so such a thing is not possible practically. For an instance imagine the distance traveled by the bird when the distance between the two trains is just 10m and so on till 8 9 7 till the distance is just tending to 0. Moins

Here’s how to think about it so you can do it in your head: The 2 trains are closing in on each other at 60 mph + 80 mph = 140 mph. The have to cover 300 miles before they collide. They will collide in 300miles/140 mi/hr = 30/14 = 15/7 hours = 2-1/7 hours (a little over 2 hours). The bird is flying at 100 mph for 15/7 hours. The bird flies 100 mi/hr x 15/7 hours = 1500/7 miles. That is a little over 214 miles. Ballpark Reality Check: Trains crash in a little over 2 hours so the bird flies at 100 mph for a little over 2 hours which is a little over 200 miles. Yep, sounds right. Nothing else matters. Not how far the trains go, not how far the bird goes, not what direction the bird is flying, etc. Answer the question that was asked and don’t answer a question that wasn’t asked. The question is, “How far does the bird travel?” The answer is “Exactly 1500/7 miles which is a little over 214 miles.” Moins

Afficher Plus de réponses

### Q1. Draw star pattern like. n=1. * n=2. ** * n=3. *** ** * n=4 **** *** ** * n=5. ***** **** *** ** *

15 réponses

#include int main() { int n=10,i,j,s,sum=1,l,k; for(k=1;k=1;i--){ l=sum-i; for(s=1;s&lt;=l;s++){ printf(" "); } for(j=1;j&lt;=i;j++){ printf("*"); } l=l+1; sum=l; printf("\n"); } return 0; } Moins

import java.util.Scanner; class New { public static void main(String ... args) { Scanner sc =new Scanner(System.in); int n=sc.nextInt(); int p=n-2; for(int i=1;i&lt;=n;i++) { for(int j=1;j&lt;=(p*(p+1))/2;j++) System.out.print(" "); p--; for(int k=1;k&lt;=n-i+1;k++) System.out.print("*"); System.out.println(""); } } } Moins

int main() { int i,j,s,n; scanf("%d",&amp;n); for(i=1;i&lt;=n;i++) { for(s=1;s&lt;=n-i;s++) { printf(" "); } for(j=1;j&lt;=(n+1)-i;j++) { printf("*"); } printf("\n"); } } Moins

Afficher Plus de réponses

### How to measure 9 minutes using only a 4 minute and 7 minute hourglass

14 réponses

Start both timers together. When the 4 minute timer is done, flip it. 7 minute timer will have 3 minutes left. When the 7 minute timer is done, the 4 minute timer will have 1 minute left. Now you can count to 9 minutes by simply leaving the 4 minute to expire (1 min), flip it and let it expire (4 min), flip it again and let it expire (4 min). 1 + 4 + 4 = 9 Moins

The key is understanding that you will have to use the two hourglasses together. Since this problem could be asked in many ways using different values for the hourglasses and the total amount of time, it's more important to understand how you use the tools rather than memorize a specific example. The question is used to determine those who can apply their knowledge to solve problems vs. those who memorize answers "from the book". Start both timers. After four minutes, the four-minute timer will have expired and the seven-minute timer will have three minutes remaining. Flip the four minute timer over. After seven minutes, the seven-minute timer will have expired and the four-minute timer will still have one minute left. Flip the seven-minute timer over. After eight minutes, the four-minute timer will have expired for the second time. The seven-minute timer will have accumulated one minute after it's last flip. Flip over the seven-minute timer and when it expires nine minutes will have elapsed. For extra measure, you can always throw in something like, "assuming the timers can be flipped over nearly instantly..." Moins

The gist of this problem is that the only way to get an exact time is by having an hourglass empty out completely when you declare 9 minutes are reached. So let's take a holistic approach: the most time we can count using ONE of the hourglasses is 8 minutes: by starting the 4-minute hourglass, flipping it, then letting it go for another 4-minutes. Let's say we start both the 4-minute and the 7-minute hourglass at the same time. The 7-minute hourglass runs out before 8-minutes are up, so we flip it. When 8-minutes are up (measured by the 4-minute hourglass), we know 1 minute worth of sand has deposited since the 7-minute hourglass was flipped. So now flip that sand upside and down and let it drain completely, and we've reached 9 minutes. Moins

Afficher Plus de réponses

### You have all of the prices for a given stock for the next year. You can buy once and sell once in that year. How do you determine when to buy and sell to maximize your profit?

14 réponses

Wont the optimal answer be buy at 1 and sell at 100? The profit in this case is 99, where as in ur case its 50(buy @ 1 and sell @ 50, the profit is 49). So I will choose to buy at 1 and sell at 100. Any way, 1. set sell price at last entry = 50(lets call sell price as sp), set profit = lowest possible value 2. then set i = last but one entry = 3 (ie index of 10) 3. array[i] profit so profit = diff = 40 i-- 4. i = 2 array[i] &gt; sp, so sp = 100 i-- 5. i = 1 arrayi] diff, we dont change the profit value i --; 6. i = 0; array[i] profit(40) so set profit = diff END OF LOOP ans = profit = 99. Hope this helps u Moins

Outside the box: just find the minimum/maximum. If the minimum is before the maximum, buy it and sell it as usual. If the minimum is after the maximum, short it and buy it back later. Moins

The question is equivalent to the following problem: Find a sequence with max sum in an array of negative and positive real numbers s.t the numbers in this case are the delta between two consecutive prices. Moins

Afficher Plus de réponses

### Coding test: 1. Given a string, find out if there's repeat characters in it. 2. SQL. Given a Customer table and a Payment table (with Customer ID as primary and foreign key), write a query to output a list of customers who have not paid their bills in the last 3 months.

15 réponses

Did well on the first Java coding test. Solution using Hashset. Not so well on the SQL query. Able to give a query, but a few errors in it I am sure. Also there was a second questions regarding the SQL query, which we didn't have time to get to. So that was another bad thing. I am pretty sure I did not pass. But good learning experience. Moins

import java.util.*; public class test { public static void main(String[] args) { String str = "abdc"; char[] arr = str.toCharArray(); HashSet set = new HashSet(); for (char i : arr ) { set.add(i); } if((set.size()) == (arr.length)) System.out.println("unique character"); else System.out.println("repetition"); } } Moins

No

Afficher Plus de réponses

### Why do you want to work at The Home Depot?

15 réponses

Im trying to improve my knowledge in many home improvement products. Also im a energetic customer service bubbly kinda person whom enjoys speaking with people. What better place to work than home depot? Many people come in to purchase items daily. I believe id be the perfect candidate while learning and improving my self at the same time. Moins

I have always been a do it yourselfer and been fascinated by how many things you can do to personalize your home. Moins

Because sales and customer service and satisfaction is what I really enjoy doing

Afficher Plus de réponses

### How would you determine if someone has won a game of tic-tac-toe on a board of any size?

15 réponses

I think maybe this question is worded a bit wrong, because given a tic-tac-toe board you would need to read in at least some of the values on the board to figure out if someone has won, and this would be impossible to do in constant time (the larger the board, the more values you would have to read). I think they must mean how can you determine if someone has won during a game in real time, as in checking after every move. This can be solved with a strategy in constant time. My solution would be: Create an array of size 2n+2 at the beginning of the game and fill it with zeros. Each spot in the array will be a sum of X's or O's horizontally (the first n places in the array), vertically (the second n places in the array) and diagonally (the last 2 places). Then with every move, you add 1 to the 2 places (or 3 if on a diagnol) of the array if X, and subtract 1 if its an O. After adding you check and see if the value of the array is equal to n or -n, if it is, n mean X has won and -n means O has won. I would bet there is a more elegant solution than creating a large array, but since this isn't my job interview I can't be bothered trying to figure one out. :) Moins

Happier player doesn't always mean the winner. A father teaching his son how to play tic tac toe for instance could be happier if his son actually beat him at the game. Your "simplest answer" is wrong. Moins

Assume that you are handed a board with no prior knowledge of what has happened in the game. Assume that, to win on a board of size NxN, the player must have N 'X' characters or 'O' characters in the same row, column, or diagonal. Assume that, for our problem, we are only checking if the winner is 'X'. We have to make at least one pass through the game board, but we should be able to solve the problem in one pass without checking any cell twice. Target running time O(N^2) for a board of size NxN. boolean checkXWinner(int[][] a, int n){ int[] diagonalSums = new int; int[] columnSums = new int[n]; initialize diagonalSums and columnSums with zeroes; int rowSum = 0; for (int i = 0; i &lt; n; i++) for (int j = 0; j &lt; n; j++) if (a[i][j] = 'X') rowSum++; columnSums[j]++; if (i == n-1 &amp;&amp; columnSums[j] == n) return true else if (i == j) diagonalSums++; if (i == n-1 &amp;&amp; diagonalSums == n) return true else if (i = n-1-j) diagonalSums++; if (j == 0 &amp;&amp; diagonalSums[i] == n) return true if (rowSum == n) return true Moins

Afficher Plus de réponses

### FInd the maximum sum of a sub-sequence from an positive integer array where any two numbers of sub-sequence are not adjacent to each other in the original sequence. E.g 1 2 3 4 5 6 --&gt; 2 4 6

15 réponses

Max(i) = max(Ni + Max(i+2), Max(i + 1)) public int findMaxNonAdjacentSequence(int[] nums) { int[] max = new int[nums.length + 2]; for (int i = nums.length - 1; i &gt;= 0; i--) { max[i] = Math.max(nums[i] + max[i + 2], max[i + 1]); } return max; } If memory is concern, you can just keep the last two max values. Moins

It works just fine even for 1, -1, -2, -3, 3

In js: function getMaxSubsequence(arr) { var maxFrom = {}; function getMax(start, end) { var len = end-start+1, x1, maxSkip1, maxSkip2; if (maxFrom[start]===undefined) { if (len&lt;1) { return 0; } else if (len==1) { return arr[start]; } else if (len==2) { return Math.max(arr[start],arr[end]); } else { x1 = arr[start], maxSkip1 = getMax(start+1,end), maxSkip2 = getMax(start+2,end); maxFrom[start] = Math.max(x1+maxSkip2,maxSkip1); } } return maxFrom[start]; }; return getMax(0, arr.length-1); } Moins

Afficher Plus de réponses
51 - 60 sur 364 609 Questions d'entretien