# 4K

Solutions Engineer interview questions shared by candidates

## Top Interview Questions

Sort: Relevance|Popular|Date
Solutions Engineer was asked...December 8, 2015

### Given a list of integers and a target number, list all pairs that sum up to that number

Not sure what happened above an wrong code got pasted. First one: int main (int argc, char * argv[]) { vector v = {2, 3, 5, 1, 6, 9, 10}; int target = 12; map m; for(auto it = v.begin(); it!= v.end(); it++) { int diff = target - *it; if(m.find(diff) != m.end()) { cout v = {2, 3, 5, 1, 6, 9, 10}; int target = 6; unordered_map m; //essentially a hash table map used; //to make sure we do not print same pair twice as (a,b) and (b,a) for(auto it = v.begin(); it!= v.end(); it++) //just load from vector to hashtable for quick lookup. m[*it]++; for(auto it = v.begin(); it!= v.end(); it++) { int diff = target - *it; auto it2 = m.find(diff); if(it2 != m.end()) { if(used.find(diff) == used.end() &amp;&amp; used.find(*it) == used.end()){ //use unused pair cout &lt;&lt; *it &lt;&lt; ", " &lt;&lt; diff &lt;&lt; "\n" ; used[diff]++; used[*it]++; } } } return 0; } Less

WTH. Something wrong with the interface, I will post one by one. int main (int argc, char * argv[]) { vector v = {2, 3, 5, 1, 6, 9, 10}; int target = 12; map m; for(auto it = v.begin(); it!= v.end(); it++) { int diff = target - *it; if(m.find(diff) != m.end()) { cout &lt;&lt; *it &lt;&lt; " ," &lt;&lt; diff &lt;&lt; "\n"; } else{ m[*it]++; } } return 0; } Less

I think the solution to this problem is pretty easy. We need to take a map that monitors for the difference in it. I got the following code for it: int[] findPairs(int[] numbers, int target){ Map map = new HashMap(); int[] result = new int[2]; for(int i=0;i Less

### What port does ftp use? (That seemed like a trick question because they only wanted one port)

In FTP, Port 20 is used for the data whilst port 21 is used for commands

Port numbers 21 and 20 are used for FTP. Port 21 is used to establish the connection between the 2 computers and port 20 to transfer data. Less

20 (data) &amp; 21 (commands) for regular ftp, 989 (data) &amp; 990 (commands) for ftps. not sure why they want 20 though since the first connection is typically established on 21, unless they're specifically troubleshooting why data isn't being transferred Less

### What are the packets sent in a TCP connection?

Syn // Syn - Ack // Ack

It's either you are so confused with the question or that question actually is very vague. It is not very clear. It seems that's obviously not the actual question you heard. The interviewer is probably asking "What are the information is in a TCP packet?" - This question makes lot of sense. Less

Whoever answered it with a 3 way handshake (SYN/SYN-ACK/ACK) does not know what the question is about. Less

### Write a program which stores the results of the numbers in a Fibonancci sequence in an array

def fibRecr(n): if n==1 or n==2: return 1 return fibRecr(n-1)+fibRecr(n-2) # Eg, to 15, store the sequence in array f1 for i in range (1, 15): f1.append(fibR(i)) Less

I think the key here is to use memoization so that you don't do have to calculate same value again as again in intermediate steps. Eg. in above solution, if we say find all values till 10, you have to calculate fibR(5) for 6, 7, 8, 9, 10 and since its recursive call this will not finish for even small size of n. Here is a solution using DP/memoization #define n 10 map m; //caches the intermediate steps int fib(int x) { auto it = m.find(x); if(it!= m.end()) return it-&gt;second; if(x == 0) { m[0] = 0; return 0; } if(x == 1) { m[1] = 1; return 1; } int val = fib(x-1) + fib(x-2); m[x] = val; return val; } int main (int argc, char *argv[]) { int result[n+1]; m[n] = fib(n); for(map::iterator it2= m.begin(); it2!= m.end(); it2++) // just to convert answer in an array form as asked result[it2-&gt;first] = it2-&gt;second; return 0; } Less

I would write only the function and I assume that you will have to return the array: It goes as follows: long[] fib(int n){ long[] result= new long[n]; result[0]=0; result[1]=1; if(n&lt;2) return result; for(int i=0;i Less

### Linux: view a column in the csv in the command line

awk can be used

awk -F";" '{print \$2}' /path/to/csv_file.csv &lt;- corrected

awk -F, '{print \$1}' or cut -d, -f1 (replace '1' w/ desired column number(s))

### Given a pointer to the head of a singly linked list, iterate it backwards printing the values in reverse. Give 2 implementations - a recursive one, and an iterative one.

Recursive: if not last, call the recursive function on the next node, then print self value. Iterative: iterate over each node and push the value to a stack, then iterate over the stack printing each value. Less

### You are to write pseudo code O(n) algorithm to maximize a one day trade. You will have 5 days of predicted prices and your algorithm must choose what day to buy and sell to maximize gains.

The brute force solution O(n^2) is simple... find the all of the permutation deltas and pick the largest value. You can trim some iteration by removing the permutation that are irrelevant such as buy and selling on the same day, or selling on the previous days, etc.... I thought about using the delta and track the rise and falls... However 25 minutes goes by pretty fast so I did not put any pseudo code on the test.. In hind site I should have put down the O(n^2) solutions to show I understood the problem and probably studied Less

Hi, As a part of your interview process, were you asked to build any app ?

I think this could be the answer you might be searching for. !! int maxP(int[] a){ if(a.length == 0) return 0; int max= a[1]-a[0]; int element = a[0]; for(int i=1;imax){ max= a[i]-ele; } if(a[i] Less

### Boggle game - given a board of letters (2d array) and a word (string), return whether the word exists in the board. From each letter you can move in all directions (including diagonals), but you cannot use the same letter twice.

import java.util.*; public class Game { public boolean check(char[][]game, String s) { boolean result = false; // Compute the position of initial character HashMap&gt; hmap = new HashMap(); char[] characters = s.toCharArray(); for(int i = 0; i &lt; game.length; i++) { for (int j = 0; j &lt; game[0].length; j++) { if(characters[0]==game[i][j]) { // Call Horizontal result = HorizontalLR(characters, game, i, j); if(result) { return true; } // Call Vertical result = VerticalTB (characters, game, i, j ); if(result) { return true; } result = DiagonalLR (characters, game, i, j ); if(result) { return true; } } } } return false; } // Horizontal R-L public boolean HorizontalLR(char [] characters, char[][]game, int i, int j) { // check if the remaining length of the board is greater than or equal to length of string if(game[0].length - j &lt; characters.length) { return false; } for (int p = j, q = 0; p &lt; game[0].length &amp;&amp; q &lt; characters.length ; p++, q++) { if(characters[q] != game[i][p]) { return false; } } return true; } // Vertical Top to Bottom public boolean VerticalTB(char [] characters, char[][]game, int i, int j) { if(game.length - i&lt; characters.length) { return false; } for(int p = i, q = 0; p &lt; game.length &amp;&amp; q &lt; characters.length ; p++, q ++) { if(characters[q] != game[p][j]) { return false; } } return true; } // Diagonal L-R public boolean DiagonalLR(char [] characters, char[][]game, int i, int j) { if(game[0].length - j &lt; characters.length || game.length-i&lt; characters.length) { return false; } for(int p = i , q = j , r = 0 ; p&lt; game[0].length &amp;&amp; q &lt; game.length &amp;&amp; r &lt; characters.length; p++, q++, r++) { if(characters[r] != game[p][q]) { return false; } } return true; } public static void main(String [] args) { Game g = new Game(); char [][] game = {{'B','A','T','A'}, {'A','N','M','N'}, {'G','G','H','T'}, {'M','E','T','E'}}; System.out.println(g.check(game, "AMTE")); } } Less

Doesn't the above approach have a time complexity of pow(8,(m,n))?

Find all occurrences of the first letter in the word, then use a recursive function to move to all valid directions to the next letter until you get past the last letter. Use a hashtable to remember the used positions, or mark each letter as some unique string and revert it back later. Less

### Given a 3-digit number, find the next number using the same digits. Eg. if number is 124, code must return 214.

but the next number of 124 is 142. How it is 214?

Edit : it's 142

Hello Folks, ``` #include using namespace std; typedef long long ll; typedef long double ld; #define MOD 1000000007 int main() { int t; cin&gt;&gt;t; while(t--) { int n; cin&gt;&gt;n; vector arr(n); for(int i=0; i&gt; arr[i]; int idx = -1; for(int i=n-1; i &gt; 0; i--){ if(arr[i-1] =0; i--) cout =idx+1; end--) { if(arr[idx] &lt; arr[end]) break; } swap(arr[idx], arr[end]); sort(arr.begin() + idx +1 , arr.end()); for(int i=0; i Less

### Balance parenthesis by removal, generate fibonacci.

Remove extra parenthesis with O(N) space and time complexity. Give a good solution to fibonacci that isn't the super complicated one that would take 45 minutes to draft. Less

Fibonacci in O(n) time O(1) space: public int fibonacci(int n) { if (n == 1) return 1 int prev = 0, cur = 1; for(int i = 2; i &lt;= n; i++} { int tmp = prev; prev = cur; cur = prev + cur; } return cur; } Less

Balance Parenthesis public static String balanceParenthesis(String s) { StringBuilder str = new StringBuilder(s); int left = 0; for(int i = 0; i 0) { left--; } else { str.deleteCharAt(i--); } } } int right = 0; for(int i = str.length() - 1; i &gt;= 0; i--) { if(str.charAt(i) == ')') { right++; } else if(str.charAt(i) == '('){ if(right &gt; 0) right--; else { str.deleteCharAt(i); } } } return str.toString(); } Less