some the interview Questions asked in cadence
Ques 1. how many trailing zeros will be there in factorial 100;
Ans: 21.
Ques 2: How to implement a linked list using stack?
Ans: I think the solution is possible by using two stacks i.e. one temporary stack.
Lets say we have two stack and tempStack as and we can perform the stack operation on these using
the following functions
please dont consider is a running code; I just try to give the logic
Element pop(stackNumber);
bool push(stackNumber Element);// expecting push will return false on stack overflow
bool overflow(stackNumber);
bool empty(stackNumber);
now the lets see how can we perform the operations needed for linked list.
// function to search a element in the list
bool find(Element elem) {
bool found false;
Element tempElem
//traverse the stack
while (! empty(stack)) {
tempElem pop(stack);
push(tempStack tempElem);
if(tempElem elem) {
found true;
break;
}
}
while (!empty(tempStack) {
tempElem pop(tempStack);
push(stack tempElem);
}
return found;
}
// function to insert element in the list after a given element
bool insertAfter(Element elem Element targetElem) {
Element tempElem
while (!empty(stack)) {
tempElem pop(stack);
push(tempStack tempElem);
if(tempElem taggetElem) {
break;
}
}
if(!push(stack elem)) {
retrun false; // for stack over flow
}
while (!empty(tempStack) {
tempElem pop(tempStack);
if(!push(stack elem)) {
retrun false;// for stack over flow
}
}
return true; // success
}
//function to delete an element from a list return false if stack is empty
bool deleteElem(Element elem) {
bool found false;
Element tempElem
while (!empty(stack)) {
tempElem pop(stack);
//if element to delete is found then dont push it to temp stack
if(tempElem elem) {
found true;
break;
}
push(stack2 tempElem);
}
while (!empty(tempStack) {
tempElem pop(tempStack);
push(stack elem))
}
retrun found; //false if the elemet does not exist
}