SK
BAN USER
- 0of 2 votes
AnswersGiven a List of Strings, return the number of sets of anagrams.
- SK in United States
For instance, given:
<abc,cab,dac,beed,deb,few,acd>
return 5| Report Duplicate | Flag | PURGE
Google Algorithm - 0of 0 votes
AnswersThe following is code that defines a modified recursive implementation of the fibonacci sequence:
fib (n) { if (n == 0) { print (“0”); return 0; } if (n == 1) { print (“1”); return 1; } return fib(n-1) + fib(n-2); }
We see that whenever we reach 0, we print out a 0 and when we reach 1, we print out a 1.
- SK in United States
Write an efficient algorithm that will print how many times 0 and 1 are printed out when a given n is input to the fibonacci function.
Examples:
numberOfOnesAndZeros(2) -> One “1” is printed out, One “0” is printed out
numberOfOnesAndZeros(3) -> One “1” is printed out, Two “0”s are printed out
numberOfOnesAndZeros(14) -> 233 “1”s printed, 377 “0”s printed| Report Duplicate | Flag | PURGE
Student - 0of 0 votes
AnswersLet's define an array as "pointy" if it monotonically increases from element 0 to some element k and then monotonically decreases from k onwards. (0 <= k).
- SK in United States
Example: [1,2,3,4,5,4,3,1]
Not Pointy: [1,2,3,5,3,6,1]
Given an array "s", modify it so that it is pointy.| Report Duplicate | Flag | PURGE
- 0of 0 votes
AnswerFind the intersection of 2 binary search trees. Remember that this is a set.
- SK in United States| Report Duplicate | Flag | PURGE
Algorithm - 2of 2 votes
AnswersYou are given a String of number characters (S), like the following for example:
- SK in United States
"132493820173849029382910382"
Now, let's assume we tie letters to numbers in order such that:
A = "0"
B = "1"
C = "2"
...
M = "12"
N = "13"
...
Y = "24"
Z = "25"
Write an algorithm to determine how many strings of letters we can make with S, by converting from numbers to letters.| Report Duplicate | Flag | PURGE
Google - 0of 0 votes
AnswersWe can find the minimum of an integer array in n operations. We can find the maximum of an integer array in n operations.
- SK in United States
How can we find both the min and max of an integer arrays in less than 2n operations?
Hint: Specifically in 3n/2 + c operations| Report Duplicate | Flag | PURGE
- 0of 0 votes
AnswersGiven two binary trees, A and B,
- SK in United States
we can define A as a subset of B if the root of A exists in B and if we can superimpose A on B at that location without changing B. (That is, the subtrees from the root of A and the node in B that is the same as the root of A are the same),
Example:
A:
5
4 7
B:
6
5 12
4 7 8 10
A is a subset of B in this case.
B1:
6
5 12
4 7 8 10
1
A is still a subset of B1, even though B1 extends one more level than A.
Write an algorithm to determine if one tree is a subset of another tree| Report Duplicate | Flag | PURGE
Google Algorithm
One way you can do this is make nodes for each number, then add connections in code.
Put all of these nodes into a List (L)
Now, run DFS on one node- All nodes in the visited list of this DFS will be part of a set (group), Now, remove all the nodes in the visited list from L. With whatever node is left in L,run DFS and repeat the same process until L is empty.
Not the most efficient code, but it's the easiest to implement and explain:
basically loop until you reach the integer that has k counts of the digit before it. record it as the beginning of your possible range. then, find the next integer that produces a count of digits before it that exceeds k, find the number right before that and set it as the ending of the possible range
public int[] range(int d, int k) {
int[] range = new int[2];
int count = 0;
int i = 0;
while (true) {
int temp = numPresent(i, d);
if (temp > 0 && count <= k ) {
count += temp;
range[0] == i;
}
if (temp > 0 && count > k ) {
count += temp;
range[1] == i-1;
break;
}
i++;
}
return range;
}
int numPresent(int n, int digit) {
//returns the number of digit in the integer n
}
Almost.
Let's define ascii values:
A= 65
B = 66
C = 67
D = 68
E = 69
... etc.
If we have two strings "BE" and "DC", they both sum to the same value, but they have different characters.
I think instead of transforming based on the sum of characters, when you iterate over the list, the hash code should be the hashcode produced when the particular string is lexicographically ordered.
so if you have the following strings:
"BAC", "CAB"
we would first find the lexicographically sorted version of the string , (for both this is "ABC") and in the hash location for "ABC", we would add both of them
Something like the following:
public Node makeBST(Node head) {
if (head == null) { return null; }
Node mid = findCenter(head);
mid.previous.next = null;
mid.previous = makeBST(head);
mid.next = makeBST(mid.next);
return mid;
}
public Node findCenter(Node head) {
//implement
}
This sounds like unweighted interval scheduling (which is greedy):
Order all the events on the calendar by their ending time in a list. Then,
loop through all of the events. For each event, remove all events in the list that it intersects, then continue to the next event in the list and repeat the process.
If there are certain events that are more valuable (more weight), then do the weighted interval scheduling algorithm which uses dynamic programming
This sounds like unweighted interval scheduling (which is greedy):
Order all the events on the calendar by their ending time in a list. Then,
loop through all of the events. For each event, remove all events in the list that it intersects, then continue to the next event in the list and repeat the process.
If there are certain events that are more valuable (more weight), then do the weighted interval scheduling algorithm which uses dynamic programming
Bouncing off of this, i was thinking that maybe the following would optimize?
Sort list, from negative to positive. For each negative number, try all subsets of positive numbers that equal the negative number. For each positive number, try all subsets of negative numbers that equal the positive number.
Thus, instead of trying all possibilities, the run time becomes:
nlogn + m * 2^n + n * 2^m
such that:
n = # of negative numbers
m = # of positive numbers
assuming the list is balanced b/w positive and negative numbers, this can severely cut down running time to approximately peak at:
nlogn + 2^(n/2)/2 + 2^(n/2)/2 = nlogn + 2^(n/2), which is approximately 33millionish (bad for int, but good for long)
Basically you can do an inorder traversal, but keep track of a global 'previous' node. on each node we visit in the traversal, set the previous node's next pointer to the current node we are on. the current node's previous pointer to the previous global node. then set the current node as the global node and continue the traversal
Node global;
public void linkedList(Node n) {
if (n == null) { return; }
linkedList(n.left);
if (global == null) {
global = n;
return;
}
global.next = n;
n.previous = global;
global = n;
linkedList(n.right);
}
public char[] sort(char[] x) {
char[] out = new char[x.length];
int a, b;
a = 0;
b = x.length - 1;
for (int i = 0; i < char.length[]; i++) {
if (x[i] == 'h') { out[a++] = 'h'; }
if (x[i] == 'l') { out[b--] = 'l'; }
}
while (b > a) {
out[b--] = 'm';
}
return out;
}
2 hash tables. 1 keeps track of Node addresses and the other keeps track of the number of duplicates of a certain value.
O(n) time complexity and O(2n) space complexity-
HashTable A <address, boolean (visited already?)>
HashTable B <value, number that this value has occurred in our traversal>
Reppaulajheineman, Consultant at Bank of America
Hello, I am from Las Vegas, NV.Completed a Diploma course in Advanced Technological Developments and Staff Management. I have ...
Use a stack:
We loop through the string. If we encounter an open parenthesis, we put it on the stack. If we find a close parenthesis, we remove an open parenthesis of the stack- In this case, if we find that there is no open parenthesis on the stack, we know that we have invalid parentheses.
Likewise, If we find that by the end of the string, we still have open parentheses on the stack, it is invalid.
Some code:
- SK January 17, 2015