thewhatwhat
BAN USER- 0of 0 votes
AnswersImplement a method for the following signature:
void * alignedAllocate(size_t sizeInBytes, size_t alignment) { }
The method should allocate memory for the given size and the pointer should be aligned.
For example ifp = alignedAllocate(1000,64);
p%8 should be 0.
- thewhatwhat in United States
Implement a second method that deleted the pointer give p.
Extend the delete method to handle multiple p's.| Report Duplicate | Flag | PURGE
Google Software Engineer C C++
It's got to do with pointer's memory value. A pointer points to any byte value in memory. 64 bit alignment means the pointer should be pointing to a value that is divisible by 8. You get 8 by dividing 64/8. 64 is bits, you have to convert that to bytes. Read up more about C++ memory alignment. For example it helps pack member data in classes.
- thewhatwhat September 09, 2015I wrote similar code except I used new instead of malloc. I did a new with char* and cast to void* but apparently I did something wrong cause I didn't get selected.
- thewhatwhat September 06, 2015x is unsigned though. -x gives a compilation error in C++
- thewhatwhat September 01, 2015Huh?? What am I missing here? Call readNextNumber() till it hits null; while reading push data to a vector (talking C++). Sort the vector, divide size by 2 and you have the median. Sorting takes the longest time - O(nLNn)
- thewhatwhat August 28, 2015Now sort the vector and make a number starting from the end of the vector. If A's length is a then time is O(aLNa) for the sort and others are O(a)
- thewhatwhat August 22, 2015None of you guys have discussed about how to break an int into individual numbers. I have the following code:
int AsBiggestNumber = 0;
int pos = 0;
int count = 0;
vector<int> numbers;
while (true) {
int remainder = A%10;
if (AsBiggestNumber<remainder) {
AsBiggestNumber = remainder;
pos = count;
}
if (remainder==0)
break;
numbers.push_back(remainder);
count++;
A=A/10;
}
Treat each point as a node in a graph and each node is connected to each other. For example, if we have 4 points, A, B, C, D then we have AB, AC, AD, BC, BD and CD. So we pick any node and do a DFS/BFS and each "step" is a line.
- thewhatwhat August 20, 2015Sort and traverse. nlogn + n = nlogn
- thewhatwhat July 15, 2015I'm just curious as a guy studying for the google interview, did you just come up with this idea in one-shot?
- thewhatwhat July 12, 2015string longestCommonPrefix(vector<string> strings) {
if (strings.size()==0) return "";
string prefix = strings[0];
for (size_t i=1; i<strings.size(); ++i) {
char *prefixIter = &prefix[0];
char *strValIter = &((strings[i])[0]);
int count = 0;
while (*prefixIter == *strValIter) {
++count;
++prefixIter;
++strValIter;
}
prefix = strings[i].substr(0,count);
}
return prefix;
}
Similar to binary search but had to change the return condition and when setting high and low, they're set to mid rather than mid-1/mid+1.
int binarySearchForPivot(vector<int> &data) {
size_t low = 0;
size_t high = data.size()-1;
size_t mid;
while (low<=high) {
mid = low + (high-low)/2;
if (data[mid]>data[mid+1]){
return mid;
}
if (data[low]>data[mid])
high = mid;
else if (data[mid]>data[high])
low = mid;
else
return -1;
}
return mid;
}
Doesn't taking the topological sort hinder the parallelism? For example, in the 1st university say both mt42 and cs123 are offered in fall. Taking the top sort will give you mt42 and cs123 one after the other and we've lost the parallelism. Unless we get the top sort and then on top of that parse the top sort with the semester information and the hard limit.
- thewhatwhat July 04, 2015It means find two numbers so that
a) the difference between them is 1
b) most number of elements in the array lie between them.
To explain the answer [-12.3,-13.3], the difference between them is 1. Also -12.3, -12.5 and -12.9 lie between them. Similarly [-12,-13] also could've been an answer since it still would include those 3 numbers. But if we considered [-13,-14] then it will include only -13.7 so not a good answer.
I think they wanted the whole array in a sorted order. Not just the minimum number.
- thewhatwhat June 11, 2016