↳
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
↳
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
↳
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
↳
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
↳
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))
↳
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
↳
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
↳
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
↳
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
↳
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 <= 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 >= 0; i--) { if(str.charAt(i) == ')') { right++; } else if(str.charAt(i) == '('){ if(right > 0) right--; else { str.deleteCharAt(i); } } } return str.toString(); } Less