Sunday, June 19, 2011

List Vs Linked List

http://stackoverflow.com/questions/169973/when-should-i-use-a-list-vs-a-linkedlist


Linked lists provide very fast insertion or deletion of a list member. Each member in a linked list contains a pointer to the next member in the list so to insert a member at position i:
  • update the pointer in member i-1 to point to the new member
  • set the pointer in the new member to point to member i
The disadvantage to a linked list is that random access is not possible. Accessing a member requires traversing the list until the desired member is found.

Wednesday, June 15, 2011

.NET CORE


Difference Between HashTable and Dictionary:
Simply, Dictionary<TKey,TValue> is a generic type, allowing:
  • static typing (and compile-time verification)
  • use without boxing
If you are .NET 2.0 or above, you should prefer Dictionary<TKey,TValue> (and the other generic collections)
A subtle but important difference is that Hashtable supports multiple reader threads with a single writer thread, while Dictionary offers no thread safety. If you need thread safety with a generic dictionary, you must implement your own synchronization or (in .NET 4.0) useConcurrentDictionary<TKey, TValue>.

SQL - Interview Questions

Can one retrieve only the Nth row from a table?
SELECT * FROM (
   SELECT name,ROWNUM RN FROM test_employee WHERE ROWNUM < 8 )
 WHERE  RN = 7;


http://www.orafaq.com/wiki/ROWNUM


Any other efficient way:
--returns the 7th row, assuming minimum 7 rows exists
SELECT rownum,name,id,mid FROM test_employee a
 GROUP BY rownum,name,id,mid HAVING rownum = 7;


--returns the fourth row, assuming minimum 4 rows exists
SELECT * FROM test_employee WHERE rownum=1 AND rowid NOT IN
(SELECT rowid FROM test_employee WHERE rownum <= 3);


--returns 8th row, assuming, min of 8 rows exists
SELECT * FROM test_employee t1
WHERE  rowid = (
        SELECT rowid FROM test_employee t1
        WHERE  rownum <= 8
        MINUS
        SELECT rowid FROM test_employee t1
        WHERE  rownum < 8);


--Adding a column after a specific column
ALTER TABLE tablename ADD columnname AFTER columnname;


CREATE TABLE tab1 AS SELECT 0 AS col1, col1 AS col2 FROM tab1_old; .
alter table test_employee add  dept_id number;
alter table test_employee add  sex varchar2(1);


--to find the males and females in a company
select  sum decode(sex,'M',1,0)) MALE,
             sum decode(sex,'F',1,0)) FEMALE,
             count(decode(sex,'M',1,'F',1)) TOTAL
  from test_employee;


--To find the number of males, females department wise
select dept_id, sum decode(sex,'M',1,0)) MALE,
             sum decode(sex,'F',1,0)) FEMALE,
             count(decode(sex,'M',1,'F',1)) TOTAL
  from test_employee
 group by dept_id;


--2nd max from the table
select max(id) from test_employee where id <
(select max(id) from test_employee);


Implementing IF-THEN-ELSE login in select statement:
One can use the CASE statement or functions like DECODE, NVL, NVL2, NULLIF, COALESCE, etc. 

SQL - Update Statements



Scenario: (Uses Oracle-SQL)


HR asks to increment the salary


1. <= 3000, increase by 1000;
2. <= 4000, increase by 500;
3. <= 5000 increase by 250;


Example:
Current Salary: 3000 then New Salary should be 3000 + 1000 + 500 + 250 = 4750


Solution:





create table emp_salary
(
  id               number,
  name             varchar2(20),
  current_sal      number(20,2),
  new_sal          number(20,2)
);


insert into EMP_SALARY (id, name, current_sal)
values (1, 'Mani', 3000);
insert into EMP_SALARY (id, name, current_sal)
values (2, 'abhishek', 4000);
insert into EMP_SALARY (id, name, current_sal)
values (3, 'ajay', 5000);
insert into EMP_SALARY (id, name, current_sal)
values (4, 'arun', 2000);
commit;




1. Not efficient:



update emp_salary
set new_sal = current_sal + 1000 + 500 + 250
where current_sal > 0 and current_sal <= 3000;

update emp_salary
set new_sal = current_sal +  500 + 250
where current_sal >= 3001 and current_sal <= 4000;
update emp_salary
set new_sal = current_sal +  250
where current_sal >= 4001 and current_sal <= 5000;
commit;



2. Efficient:


update test_emp_sal
set current_sal =
   case
      when current_sal > 0 AND current_sal <= 3000 then current_sal + 1000 + 500 + 250
      when current_sal > 3001 AND current_sal <= 4000 then current_sal +  500 + 250
      when current_sal > 4001 AND current_sal <= 5000 then current_sal +  250
   end
;

commit;


==================================================================================

update orders
set order_mode_num =
   decode(order_mode,
             'direct',100,
             'online',200,
             'walmart',300,
             'amazon',400,
          0)
;

==================================================================================

update orders
set order_mode_num =
   case
      when order_mode = 'direct' and
           customer_id < 102 then 500
      when order_mode = 'direct' then 100
      when order_mode = 'online' then 200
      when order_mode = 'walmart' then 300
      when order_mode = 'amazon' then 400
      else 0
   end
;

=================================================================================



CAREER!!!

What one skill, if you developed and practiced it consistently in an excellent fashion, would have the greatest positive impact on your career?   ----Brian Tracy.

Wednesday, April 27, 2011

Computer Number System

We know and use Decimal Number System in our day to day life.  Though daily we use computers, we may not think much about how computers understand numbers and their internals.  Looks like school day topics. But it's always good to refresh few basics.


We know Binary, Octal, Hexa Decimal etc.  Let's get into more details....


http://en.wikipedia.org/wiki/Computer_numbering_formats  


It covers 
00010111101010
01237635265376
3637ABCDEF8738


Happy Numbering...Number represents God?