`
sakakokiya
  • 浏览: 507434 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C/Algorithm/Data Structure/Compiler Related Questions (1)

阅读更多
Q:   All-time Most-Popular-Question: How to reverse a single link list? (Most companies ask!)
A:    At least 2 popular/elegant ways to do this without using additional memory:
Non-Recursive & Recursive.
Tell me what’s the disadvantage of recursion? It’s damn expensive, be aware of stack overflow if the list is long.
// Non-Recursively (Most interviewers expect this sort of answer!)
Node *p  = head;  // h points to head of the linklist.
If( p == NULL)
return NULL;
link *h;  //C++ style C programming
h = p;
if(p->next)
p = p->next;
h->next = NULL;   // h is now an isolated node which will be the tail node eventually
while( p != NULL) {
link *t = p->next;  //tmp node.
p->next = h;
h = p;
p = t;
}
return h;  //h points to the new Head.
//-END-
// Recursively, In case you forgot.
reverse_ll(struct node ** hashref) {
struct node * first,  last;
if(*hashref == NULL) return -1;
first = *hashref;
rest = first->next;
if(rest == NULL) return;
reverse_ll(&rest);
first->next->next = first;  //reversion happens.
first->next = NULL;
} //-END-
Collateral Questions: Why recursion is evil?
A: Potential of Stack overflow for extensively nested recursions.
Q:   How to print a link list reversely?   (NetScreen, Netli, Yahoo!)
A:   If Recursively:
rev_ll (Node *h) {
If(!h) return(-1) ;
else { rev_ll(h->next); print(h->data);}
}
If Non-Recursively:
Same as last Q/A: reverse the link-list then print out from the new head. Complexity: O(2*N)
//Similar question:  How to reverse a string?  Complexity O(N/2); exchange firstlast, 2nd2nd-to-the-last, etc…
Q:  How to reverse doubly-linked list? (Akamai onsite San Mateo, CA)
A:          // Basically, double linklist is easier to reverse, the only 2 things need to do are:
//  tail head exchange;  prev next exchange;
Node *PCurrent = pHead, *pTemp;
While(pCurrent) {
pTemp = pCurrent->next;
pCurrent->next = pCurrent->prev;
pCurrent->prev = pTemp;
pHead = pCurrent;
pCurrent = pTemp;
}
// pHead will point to the newly reversed head of the double link-list
// -END-
Q: How to insert a node into an ASCENDly sorted double link list? (Infoblox onsite)
A:  // Note: the following code concerns all boundary conditions!!!
Assume we have a struct:
Struct Node {
Int data;
Node * prev;
Node *next;
};
//pHead, pCur, nn (new node), ppCur (pre-pCur)
if(pHead == NULL) return(0);
pCur = pHead;
while(pCur) {
if(pCur != pHead) {
ppCur = pCur->prev;  //keep track of prev node.
}
if(pCur->data >= nn->data) {
if(pCur == pHead) {  // insert at the head
pHead = nn;
nn->prev = NULL;
nn->next = pCur;
pCur->prev = nn;
} else { // insert at non head.
If(pCur->next == NULL) { // insert at the tail.
nn->next = NULL;
pCur->next = nn;
nn->prev = pCur;
} else { // insert somewhere in the middle.
nn->next = pCur;
pCur->prev = nn;
ppCur->next = nn;
nn->prev = ppCur;
}
return(pHead);  // return head of double-linklist.
}
} else { // keep going!
pCur = pCur->next;
}
} // end of while()
Q: How to find a loop in a linked list (single link-list)/ Yahoo! Favorite onsite question.
A:
Node *p1, *p2, *head;
p1 = p2 = head;
do {
p1 = p1->next;
p2 = p2->next->next;
} while ( p1 != p2 && p1->next != NULL && p2->next != NULL && p2->next->next != NULL )
if ( p1->next == NULL || p2->next == NULL || p2->next->next == NULL) {
printf(“None loop found!\n”;
} else if ( p1 == p2 && p1 != NULL ) {
printf(“Loop found!\n”;
} // -END-
//Story Telling:  A Yahoo! Director ever asked this question and he challenged me with this scenario: What if p1 and p2 fall into an endlessly looping hole?  This imagined scenario/dilemma will NEVER happen as a simple reply! Don’t be afraid to stand off. This also propelled me to think what made one a Technical/Engineering Director? Stupidness, Arrogance or Ignorance? Maybe all.
Q:  Print UNIQUE Array Element  (PayPal onsite interview question)   ????
A:      // Assume it’s a char * array
char * unique_array(char *s, int size)
{
int i = 1,  c = 1;
for(c=1; c<=size; ++c) {
if (s[c] != s[i-1]) { s[i] = s[c]; i++; }
}
s[i] = ‘\0’;  //NULL terminate the string!
return(s);
} //-END-
Q: Filter out matching char in a char * array
A:      //
char * filter_array(char *s, int size, char tgt) {
int i = 0, c;
for(c = 0; c<size; c++) {
if(s[c] != tgt) { s[i] = s[c]; i++; }
}
s[i] = ‘\0’;
return(s);
} –END-
Q:  Implement your atoi()
A:  // char *a  int i;
Int len = strlen(a);
Int i = 0, j = 0, sign = 2;  // 2 stands for ‘+’
If (a[0] == ‘-‘) {
sign = 1;
++j;
} else if(a[0] == ‘+’ ) {
sign = 2;
++j;
}
++j;
while(j < len) {
i =  10 * i + (a[j] – 48);  // 48 = 0×30 which is ASCII ‘0’
++j;
}
//-END-
//NOTE:  a-z: 0×41-5a(65-90);   A-Z:  0×61-7a(97-122)
Q: Write a function that takes in a string parameter and checks to see whether or not it is an integer, and if it is then return the integer value.
A:
int str2int(const char *str)
{
int value = 0;
int sign = (str[0] == ‘-')?-1:1;  // decide sign
int i = (str[0] == ‘-')?1:0;         // decide starting index
char ch;
while(ch = str[i++])
{
if ((ch >= ‘0′)&&(ch <= ‘9′))
value = value*10 + (ch - ‘0′);
else
return 0;  // non-integer
}
return value*sign;
}  /* end of str2int()
Q: Implement your itoa() ?
A:
Solution 1:  sprintf(s, “%d”, i);
Solution 2:  Convert every digit to ASCII naturally and dump to char * array. Or just putchar() recursively.
Q:Print an integer using only putchar. Try doing it without using extra storage.
A: // recusion is one way. (since integer is only 16 or 32 or 64 bits long)
void putlong(long ln)
{
if (ln == 0) return;
if (ln < 0) {
putchar('-');  // print - sign first
putlong(abs(ln));    // print as positive number
return;
}
putlong(ln/10);      // print higher digits,
putchar('0′ + ln%10); // print last digit(mod), ‘0′ + will convert to ASCII printable char.
} // end of putlong()
分享到:
评论

相关推荐

    Data Structure and Algorithm Analysis in C 习题答案

    这个压缩包包含了《Data Structure and Algorithm Analysis in C》一书的习题答案,虽然它不是教材本身,但可以作为学习过程中的宝贵参考资料。 数据结构主要包括数组、链表、栈、队列、树(如二叉树、AVL树、红黑...

    算法可视化AlgorithmVisualizer.zip

    /algorithm/[category]/[algorithm]/[example]/data.js此文件预定义了一个显示在可视化模块中的数据变量。/algorithm/[category]/[algorithm]/[example]/code.js这个文件实现可视化和算法。可视化模块...

    C语言头文件 algorithm

    C语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件...

    Algorithm-data-structure.zip

    "Algorithm-data-structure.zip"这个压缩包,显然聚焦于这两个关键概念,尤其针对数据结构部分,它包含了"data-structure-master"这一子文件夹,预示着我们将深入探讨数据结构的各种实现和应用。 算法,简单来说,...

    Algorithm-data-structure_algorithm.zip

    在"Algorithm-data-structure_algorithm.zip"这个压缩包中,我们可以推测其内容可能包含了使用JavaScript语言实现的各种算法和数据结构。 首先,让我们深入了解一下算法。算法可以分为不同的复杂度类别,如线性、...

    Data Structure and Algorithm

    1. 数据结构与算法的基础概念 - 《数据结构与算法》一书是专门讲述数据结构与算法的专业图书,内容涵盖广泛,适用于对数据结构与算法有一定了解基础的读者。 - 书中包括了数据结构和算法的详细解释,以及相关的...

    Algorithm-Data-Structure-And-Algorithm.zip

    在"Algorithm-Data-Structure-And-Algorithm.zip"这个压缩包中,我们很可能找到了一系列关于算法和数据结构的深入研究材料,这对于提升编程技能和优化代码效率至关重要。 数据结构,是组织和存储数据的方式,它是...

    Algorithm-Algorithm-Data-Structure.zip

    "Algorithm-Data-Structure-master"很可能是一个包含实际代码实现的项目,这样的实践项目可以帮助学习者深入理解各种数据结构和算法的工作原理,并锻炼实际编程能力。 总结来说,算法和数据结构是构建高效计算机...

    Data Structures and Algorithm Analysis in C, Second Edition.ppt.rar

    数据结构与算法,C描述。第二版,英文讲义PPT。 包含9章:Ch04-LISTS-STACKS-QUEUES(数据结构),Chapter3-Stacks-and-Queues,Chapter5 Binary Trees, chapter6 General trees,chapter7 Sorting,chapter8 Primary...

    Algorithm-Data_Structure_and_Algorithms_Library.zip

    总之,《Algorithm-Data_Structure_and_Algorithms_Library》包含了一系列C语言实现的经典数据结构和算法,是学习和实践计算机科学基础的重要资源。通过深入理解和掌握这些内容,开发者能够编写出更加高效、优雅的...

    Algorithm & Data Structure.md

    Algorithm & Data Structure.md

    Data Structures and Algorithm Analysis in C++ 4th 原版pdf by Weiss

    The fourth edition of Data Structures and Algorithm Analysis in C++ describes data structures, methods of organizing large amounts of data, and algorithm analysis, the estimation of the running time ...

    180+ Algorithm &amp; Data Structure Problems using C++.zip

    Data Structure Problems using C++.zip" 提供的信息表明,这是一个包含180多个算法和数据结构问题的资源包,使用C++编程语言进行实现。这个压缩包可能是为了帮助学习者熟悉和练习基础到高级的算法与数据结构,从而...

    Algorithm-data-structure-and-algorithm-in-Swift.zip

    Algorithm-data-structure-and-algorithm-in-Swift.zip,swift语言的数据结构与算法实现,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。

    Algorithm-data-structure-php-clanguage.zip

    "Algorithm-data-structure-php-clanguage.zip"这个压缩包文件显然包含了关于算法和数据结构的学习资源,特别强调了PHP和C语言的实现方式。下面我们将深入探讨这两个关键概念及其在PHP和C语言中的应用。 首先,算法...

    【算法+数据结构】Algorithm+Data Structures Programs(非扫描)

    1. 数据结构的基础:在“FUNDAMENTAL DATA STRUCTURES”部分,书籍可能涵盖了以下数据结构的基础知识和实现细节。 - 数据类型的定义和概念,包括数据类型的分类和数据抽象。 - 原始数据类型,标准原始类型,子范围...

Global site tag (gtag.js) - Google Analytics