Thursday, October 23, 2008

careercup algorithm (zz)

Design an algorithm to find the kth number divisible by only 3 or 5 or 7 i.e the only factors of these numbers should be 3,5 and 7.

http://careercup.com/question?id=57139

Monday, October 20, 2008

问一道面试题(判断是不是二叉树)(zz)

发信人: sheart (test), 信区: JobHunting
标 题: 问一道面试题(判断是不是二叉树)
发信站: BBS 未名空间站 (Mon Oct 20 17:30:28 2008)

一个类似二叉树的结构,但是有可能有环,比如:

a
/\
b c
/\ |
d e |
| |
|_|

(图排版是乱的。。就是a有两个子结点b和c,b有两个子结点d和e,但同时e有一个子结点是c)
当然这种情况下就不是二叉树了。
现在给出根结点,让判断是不是二叉树。

我说的是遍历这棵“树”,把访问到的结点加到一个list里面,每新加入一个都和现有
的所有结点比较,相同就输出false。有更快的算法吗?

Sunday, October 19, 2008

算法题, 排序(zz)

发信人: bbatucd (bb), 信区: Programming
标 题: 算法题, 排序(queue)
发信站: BBS 未名空间站 (Fri Oct 17 19:49:46 2008), 转信

given a unsorted queue, sort it via using another queue and some variable.

what if use stack instead of queue?

Saturday, October 18, 2008

Mark progress

Careercup: July 22, 2008 http://www.careercup.com/page?id=1331&n=3

Free vs. Realloc

free(): release the memory allocated to the object by malloc(),realloc() or calloc(), making it avaible for future allocation.
realloc(void* ptr,size_t size): expand or reduce the size of the allocated block of memory to size. when size=0, it is equal to free. when ptr is NULL, it works the same as malloc. when the size is larger than the old block, then newly allocated portion are unknown.

Declaration vs Definition

This is a topic that has created a lot of confusion in internet forums. Let me put it clear here. Reply me, if you don't understand after reading this.

Declaration and definition have different meanings in different contexts. Generally, a declaration describes to the compiler to bind an identifier with a specific data type. Compiler does NOT allocate storage when it sees a declaration.
A definition tells the compiler to allocate storage for the declared data type.

Function Declaration vs Definition
----------------------------------
Declaration of a function means telling to the compiler about the function's name, arguments and return type. This is done by the prototype of the function.
Eg:

int func(int x, float y) ();

Definition of a function means telling to the compiler about the details of a function and what it should do when the function is called. This is when, compiler allocates storage.

Eg:

int func(int x, float y) {
char response;
int value;
....
....
return value;
}

Variable Declaration vs Definition
----------------------------------
For automatic, static & register storage class variables, declaration and definition are the same. There is no difference between both terms. By that, I mean, the binding of identifier with data type and allocation of storage will happen at the same time.

But, when it comes to an extern variable, their meaning differ. For example, when you write
extern int i; /* extern declaration of i */
it WONT allocate storage. It tells to the compiler that a variable called "i" of integer type has already been defined (binding int data type with variable "i" and allocating space for it) in some other file. So, connect me to that memory. This is basically a linking process and is taken care by the linker.
Thus definition and declaration hold different meaning, when it comes to extern storage class variables.

Reverse Binary Tree

Node[] Reverse(Node root)

so for the tree below, out put shd be Array with 4 roots L1,L2,L3,L4

Input Tree:

R
C1 C2
L1 L2 L3 L4

Output tree: (Its No more binary tree, looks like a 4 linked list where L1,L2 share common Node C1 and R, and L3 L4 has common node C2 and R)

L1 L2 L3 L4
C1 C2
R