Tuesday, August 18, 2015

sleep() vs wait()

Thread.sleep() is used for time-synchronization.
Thread.sleep(5000); // no synchronization needed

object.wait() is used for multi-thread-synchronization.
synchronized (lock) {
  while (!condition) {
    lock.wait(); // releases lock
  }
}
synchronized (lock) {
  lock.notify();
}

When to write your own exceptions

When you need to differentiate between different types of errors.

Exception class only tells you something is wrong but not what.

Allows code to ignore certain types of errors at certain points in code and let other parts handle it.

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');