Wednesday, April 21, 2010

Exception vs status returns

From: http://nedbatchelder.com/text/exceptions-vs-status.html

Exceptions provide richer error information

Status codes take valuable channels: 1 return value

Cannot return status codes in implicit code: constructors/destructors

Status codes can go unchecked

Exceptions can cause explicit complexity

Exceptions are invisible in the source code

Exceptions create too many possible exit points for a function


Tuesday, April 20, 2010

Hashtable (hashmap)

Description
a data structure that uses a hash function to efficiently map certain identifiers or keys (e.g., person names) to associated values (e.g., their telephone numbers). The hash function is used to transform the key into the index (the hash) of an array element (the slot or bucket) where the corresponding value is to be sought.

Advantages
+ Speed

Drawbacks
- Hard to get efficient hash function
- Hard to enumerate values

Example
+ Person name to phone number lookup
- Spell-check application - not sorted

Monday, April 19, 2010

Find the largest subsquence

Difference between non-virtual and virtual function

Non-virtual function calls are resolved at compile time. Virtual function calls are dynamically resolved.

Friday, April 16, 2010

sql query

Given the following tables, write a query to display all people who do not like the bears.
create table PEOPLE (
NAME varchar(20),
primary key NAME
);
create table TEAM_LIKES (
NAME varchar(20),
LIKES varchar(20),
foreign key NAME references PEOPLE (NAME)
);
Answer:
select distinct PEOPLE.NAME from PEOPLE where PEOPLE.NAME not in (select
NAME from TEAM_LIKES where LIKES='bears');

Monday, April 12, 2010

Stack direction

From: http://kstruct.com/2007/04/16/interview-questions-stack-direction/

Stack Growth : How would you find out if a machine’s stack grows up or down in memory?

Well, it’s fairly simple to write a C program to test this. Obviously I’m assuming that a C compiler is available. The following program creates an integer (a) on the stack, then passes a pointer to a down to the function sub. sub creates another integer on the stack (b), then compares the address of a to the address of b. If b’s address is greater then a’s, then the stack is growing up, if it’s less, then it’s growing down.

#include <stdio h>

void sub(int *a) {
int b;

if (&b > a) {
printf("Stack grows up.");
} else {
printf("Stack grows down.");
}
}

main () {
int a;
sub(&a);
}