Varun
BAN USER- 1of 1 vote
AnswersU are given a set of strings, using the characters of the given string, the program needs to find the maximum length string that doesn't has any of the given string as its sub-string. In case, if we can have infinite long string satisfying the property, print -1.
- Varun in India
I solved this, but looking forward to some convincing algo.
eg 1:
11, 00
longest: 010101010101010101010101... (-1, as infinite long)
eg2
101, 111, 00, 110
longest:010(i think, maybe wrong)| Report Duplicate | Flag | PURGE
Tech Lead Algorithm - 0of 0 votes
AnswersGiven an array of n elements. The array can have duplicates.
- Varun in India
Now sort the array based on frequency of distinct number in array.
eg: a= [3,4,2,5,3,3,4,2,1,5}
output = {3, 4,2,5,1} coz, 3 occurs thrice, 4,2,5 occurs twice, so they are retained in order of there occurrence in original array, and at last 1, as it appears least number of time.| Report Duplicate | Flag | PURGE
Tribal Fusion Software Engineer / Developer Algorithm - 0of 0 votes
AnswerAsked few times over phone and in-person.
- Varun in India
Asking to get my facts correct and consolidate the info for me and for everyone here.
How's synchornization differs in multi-core vs multiprocessor arch?
Little internals about them.
How the code design and implementation changes for these two arch?
pour in your suggestions and any nice read if u know any.| Report Duplicate | Flag | PURGE
Software Engineer / Developer Computer Architecture & Low Level - 0of 0 votes
Answersint a[10]; a[-1] = 20;
where is 20 stored?
- Varun in India
I said this won't compile in C under normal circumstances, he agreed, and then said let's assume C allows this, what happens?? I said maybe then previous local variable of the function will get overwritten with this value.?? Nay insights?| Report Duplicate | Flag | PURGE
Hewlett Packard C - 0of 0 votes
Answersdefine a struct imitate ip_header as it is in a packet.
- Varun in India
actually, he wanted to see how i handle things with aren't a byte long or odd number of bytes long, like flags, ihl, version and all.
what's the best struct to define a ip_header, and tcp header.| Report Duplicate | Flag | PURGE
Samsung Software Engineer / Developer C - 0of 0 votes
Answerswrite a program to convert char *mac_address to int array.
- Varun in India
eg mac_address ="ab:cd:ef:12:34:56" to
result= [ab, cd, ef, 12,34,56]
I was able to do it, but wasn't sure, how do we store hex values in int array. i mean a[0]=a, will essentially be a[0]=10, can we store hex directly in array, so that when we do memcmp or something, we can directly compare hex against hex?
Not sure if me making sense here, still let me know, if you have suggestions.| Report Duplicate | Flag | PURGE
Samsung Software Engineer / Developer C - 0of 0 votes
Answerscome up with a solution, where u r given a single pointer in a single link list and u shoudl be able to return (+/-)nth node from it.
My solution:list * returnN (list *node, int n) { if (n>0) { traverse and reverse list from node to nth node } else { traverse and negatereverse from node till nth node }
he asked for algo only, so this worked fine.
- Varun in India
eg list=1->2->3->4->5
returnN(1, 3)
output 4
list=1<-2<-3<-4->5
returnN(4, -2)
output 2
list: 1<-2->3->4->5
Any other suggestions??| Report Duplicate | Flag | PURGE
IBM Software Engineer / Developer Algorithm - 0of 0 votes
Answers2. write sizeof operator.
I gave the code as#define sizeof(data) {data *p=0; size = abs((p+1) - (p))
He was fine with this.
- Varun in India
But then how would we extend this to find sizeof of variables???
PS: sizeof in C that we use works well for both datastruct and variable.| Report Duplicate | Flag | PURGE
Samsung Software Engineer / Developer C - 0of 0 votes
Answerswhat is the return value of the function that returns int, but isn't returning anything explicitly.
for instance output ofint fun() { printf("\ncrap"); } void main() { printf("\n return value of fun %d", fun()); }
i said, should be exit status of 0(success) or value in EPI register, but looks like i wasn't corret. He said, this has something to do with stack unwinding??
- Varun in India
Any insights?| Report Duplicate | Flag | PURGE
Samsung Software Engineer / Developer C - 0of 0 votes
AnswersAssume you have a file with 5Mb size.
- Varun in India
Each line in file has a interger. (and only one integer per line).
Now, your system can process only 1Mb data at a time in memory, that is you cannot access more than 1Mb of data at a time.
Write a program that sort the file under this circumstances.
Answer I gave:
This problem is basically, you have array of 100 number, and at time you can access only 10 of them. Then sort the array.
He was happy to see this analogy, and then my algo.
Divide the array into 10 segments of each, and sort each one of them individually.
Now, take the first element of each sort subarray and find the min.
Put this min into differnt array of the size 100 at its starting location.
Repeat this process, for the next element from the subarray which had the smallest element as identified earlier.
He seemed to be ok with this.
Any other approach?| Report Duplicate | Flag | PURGE
NetApp Software Engineer / Developer Algorithm - 0of 0 votes
Answersmain() { char *c; printf("\n%d", *c); printf("\n%d", c); printf("\n%d", &c); }
output of above program.
- Varun in India
*c some junk value, c some memory address, &c address where the pointer is stored.
Q. if we re-run the program, then what's the output.
A. all output may change.
He stressed that &c won't change, everything else might..but &c will remain same.??
I just tried on my unix box, and saw it to change?| Report Duplicate | Flag | PURGE
NetApp Software Engineer / Developer - 0of 0 votes
AnswersThis is incremental.
Q1. Write the the algo for linked list traversal.
Q2. Now, modify this to give the index of a element "x" in list (index== its location in list, rather number of comparison needed to finally get to X, else 0)
Q3. modify and give the prototype of function will work generally (that means a function prototype which takes any data strcutre(list, q, tree, heap), X, and return the count)
Q4. Now declare the function defined above.
Answers I gave:
1.bool traverse ( list * head) { while (head) {head=head->next;} return TRUE; }
2. bool findx (list *head, void *x, int num_bytes)
- Varun in India
{
bool ret = FALSE;
while (head) {
if (memcmp(head->data, x, num_bytes)){ ret=TRUE; return ret;}
else ret;
}
3. int findX(void *head, int (*traverse(void *node, void *x))
where *traverse is a function pointer that can be changed depending upon data structure for which we need to find the index of X.| Report Duplicate | Flag | PURGE
NetApp Software Engineer / Developer Algorithm
Following logic should work i guess.
oldnum = num
newnum = num;
Move from MSD(most significant Digit) to LSD(Least significant Digit)
If (current_digit <= 4)
{
replace(num, current_digit, 4);
newnum = num;
}
else
{
if ((newnum - oldnum) < 0)
replace(num, current_digit, 7);
else replace(num, current_digit, 4);
newnum = num;
}
replace() replaces in num, the digit at current location with 4 or 7(as mentioned, after current).
- Varun April 18, 2014This won't work always.
Looks you assuming there's only one route to grey grid from (0,0)
IN case there's more than one route to grey grid, then for each route to grey grid we need to traverse frm grey to end. Simply choosing one route and trying to get end from grey might not always succeed.
Anyway, there's no fuss needed about grey grid and all.
Simply use your code to traverse from (0.0) to destination grid, and check if any of the grid in path is grey, if yes this is the path, else backtrack.
Yes, doesn't look you covering all travel directions
in short something like
bool getPath(r,c)
{
bool ret = false;
int oriColor;
if (r<0 || r>N || c<0 || c>N) return false;
if (r==N && c == N)
{
if (grey)
{
print("the path is\n");
return true;
}
else return false;
}
oriColor = Grid[r][c];
if (Grid[r][c] == GREY) grey = true;
Grid[r][c] = visited;
ret = getPath(r, c-1);
if (ret)
{
print(%d %d", r, c-1);
return true;
}
ret = getPath(r, c+1);
if (ret)
{
print(%d %d", r, c+1);
return true;
}
ret = getPath(r-1, c);
if (ret)
{
print(%d %d", r-1, c);
return true;
}
ret = getPath(r+1, c);
if (ret)
{
print(%d %d", r+1, c);
return true;
}
ret = getPath(r+1, c-1);
if (ret)
{
print(%d %d", r+1, c-1);
return true;
}
ret = getPath(r+1, c+1);
if (ret)
{
print(%d %d", r+1, c+1);
return true;
}
ret = getPath(r-1, c-1);
if (ret)
{
print(%d %d", r-1, c-1);
return true;
}
ret = getPath(r-1, c+1);
if (ret)
{
print(%d %d", r, c-1);
return true;
}
if (!ret)
{
Grid[r][c] = oriColor;
if (oriColor == grey) grey = false;
}
return false;
}
Can be done i think, using following approach. Not tried though.
void deleteDuplicate (list *head)
{
int isListChanged = 0, count = 0;
list *temp, *temp1, *prev;
temp = temp1 = NULL;
do
{
count++;
isListChanged = 0;
temp = temp1 = head;
do
{
for (i=0;i<count;i++)
{
temp1 = temp1->next;
}
if (temp->data == temp1->data) isListChanged = removeNode(temp1);
temp = temp->next;
}while(temp1);
} while (isListChanged);
}
Basically, Idea is to use two pointers, one is temp which runs only to next every time, and other pointer is temp1 moves to by 2 in first iteration, 3 in second iteration, 4 in fourth and so on, until see some node in list is removed.
- Varun August 22, 2013Are we sure on this?
Virtual memory surely allows to load bigger programs into memory, but the malloc allocates a new memory of the needed size frm the heap for the program, and it will not succeed
Static allocation wil be successful, but dynamic runtime allocation I believe will fail.
I thought about the same solution, but then realized the second half of the solution won't work in O(n) a simply chcking the next two consecutive number won't do here.
Please recheck, rather we need to scan the index array, and see, if any triplet exist such that a<b<c
Is subarray has to be contiguous then the following should do
void findsubarray (int arr[])
{
int length = sizeof(arr)/sizeof(a[0]);
int A[length];
int i, j;
A[0] = arr[0];
for (i=1;i<length;i++) A[i] = A[i-1] + arr[i];
for (i=0;i<length;i++)
for (j=i;j<length;j++)
{
if ( (arr[i] + A[j]-A[i]) % length) continue;
else
{
//arr[i].......arr[j] is the needed subarray
}
}
}
As suggested above, apply the given algo and get the shortest path between A,B.
Now, in place of removing all edges one by one, why not, remove the shortest edge, and get the new path. That should be /can be the second shortest. I mean major shortest path algo are greedy ones, so this should suffice.
In case where post this we aren't able to find a path itself, then remove the next shortest edge and so forth until we get the path.
* Given a sorted array find two num that sum to given number */
void findelementsthatsum(int *array, int length, int sum)
{
int head = 0;
int tail = length - 1;
int temp = 0;
while (head <= tail)
{
temp = array[head] + array[tail];
if (sum > temp) head++;
if (sum < temp) tail--;
if (sum == temp) printf("\nTHe needed pair is :%d, %d", array[head++], array[tail]);
}
}
There's no mention if we cannot use extra memory, so its easier to use stack.
- Varun April 03, 2017Scan list 1 and push to stack 1.
Scan list 2 and put to stack 2.
Now, start popping from stk1 and stk2 , add the numbers, save carry if any, and store result in a list.
Once the stacks are completely popped, the new list is the answer!