# Solutions engineer Interview Questions in United States

# 4K

Solutions Engineer interview questions shared by candidates### Given a list of integers and a target number, list all pairs that sum up to that number

9 Answers↳

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() && used.find(*it) == used.end()){ //use unused pair cout << *it << ", " << diff << "\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 << *it << " ," << diff << "\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)

7 Answers↳

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) & 21 (commands) for regular ftp, 989 (data) & 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?

6 Answers↳

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

6 Answers↳

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->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->first] = it2->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<2) return result; for(int i=0;i Less

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

4 Answers↳

awk can be used

↳

awk -F";" '{print $2}' /path/to/csv_file.csv <- 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.

4 Answers↳

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

↳

void printrecursive(node *head) { if(head == NULL) return; else { printrecursive(head->next); cout data s; while(head!=NULL) { s.push(head->data); head = head->next; } while(!s.empty()) { cout << s.top() << " "; s.pop(); } } Less

↳

Iterative void printiterative(node *head) { stack s; while(head!=NULL) { s.push(head->data); head = head->next; } while(!s.empty()) { cout << s.top() << " "; s.pop(); } } 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.

4 Answers↳

import java.util.*; public class Game { public boolean check(char[][]game, String s) { boolean result = false; // Compute the position of initial character HashMap> hmap = new HashMap(); char[] characters = s.toCharArray(); for(int i = 0; i < game.length; i++) { for (int j = 0; j < 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 < characters.length) { return false; } for (int p = j, q = 0; p < game[0].length && q < 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< characters.length) { return false; } for(int p = i, q = 0; p < game.length && q < 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 < characters.length || game.length-i< characters.length) { return false; } for(int p = i , q = j , r = 0 ; p< game[0].length && q < game.length && r < 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

### 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.

4 Answers↳

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

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

4 Answers↳

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>>t; while(t--) { int n; cin>>n; vector arr(n); for(int i=0; i> arr[i]; int idx = -1; for(int i=n-1; i > 0; i--){ if(arr[i-1] =0; i--) cout =idx+1; end--) { if(arr[idx] < arr[end]) break; } swap(arr[idx], arr[end]); sort(arr.begin() + idx +1 , arr.end()); for(int i=0; i Less

### What is the fastest Big O equation?

4 Answers↳

I think that should be O(1).

↳

1 < logn < n < nlogn < n^2 < 2n. Fastest is O(1) and slowest is O(2n) Less

↳

Not sure why but glassdoor replaced my less than operator with <. Replace < with less than operator for the above answer Less