1) Given a stack how to reverse the elements of the stack using push() and pop() only.
Karumanchi : pg 91
2) Implement a queue using 2 stacks
Karumanchi : pg 104
3) Implement a stack using 2 queues
Karumanchi : pg 105
1) Given a stack how to reverse the elements of the stack using push() and pop() only.
Karumanchi : pg 91
2) Implement a queue using 2 stacks
Karumanchi : pg 104
3) Implement a stack using 2 queues
Karumanchi : pg 105
2) Binary Search
1) while (low <= high)
2) mid = low + high-low/2 ; // to avoid overflow
3) Implement LRU Cache
http://www.geeksforgeeks.org/implement-lru-cache/
4) Given a string “{ab}{}{{dhk}}” write a program to find if the parenthesis are balanced.
var SingletonClass = (function(){ function SingletonClass() { //do stuff } var instance; return { getInstance: function(){ if (instance == null) { instance = new SingletonClass(); // Hide the constructor so the returned objected can't be new'd... instance.constructor = null; } return instance; } }; })();
Overloading has a different meaning in PHP. PHP does not support function overloading in the same manner as C++. However it possible to pass different number of arguments to a function using func_get_args.
function findSum() {
$sum = 0;
foreach (func_get_args() as $arg) {
$sum += $arg;
}
return $sum;
}
echo findSum(1, 2), '
'; //outputs 3
echo findSum(10, 2, 100), '
'; //outputs 112
echo findSum(10, 22, 0.5, 0.75, 12.50), '
'; //outputs 45.75
Overloading in PHP provides means to dynamically “create” properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.
Example for property overloading: __set() and __get()
Example for method overloading: __call()
As of PHP 5.4.0, PHP implements a method of code reuse called Traits.
Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way which reduces complexity, and avoids the typical problems associated with multiple inheritance and Mixins.
A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own.
trait ezcReflectionReturnInfo {
function getReturnType() { /*1*/ }
function getReturnDescription() { /*2*/ }
}
class ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}
class ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
Most MySQL indexes (PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT) are stored in B-trees. Exceptions are that indexes on spatial data types use R-trees, and that MEMORY tables also support hash indexes.
http://www.kylescousin.com/2010/09/a-simple-explanation-on-how-b-tree-database-indexes-work/
If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).
MySQL cannot use an index if the columns do not form a leftmost prefix of the index. Suppose that you have the SELECT statements shown here:
SELECT * FROM tbl_name WHERE col1=val1;
SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;
SELECT * FROM tbl_name WHERE col2=val2;
SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;
If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).
Also usually MySQL just uses 1 index per query. In rare cases it may use multiple indexes for a query. This is called index-merge. Explanation of why MySQL uses just 1 index per query :
http://dba.stackexchange.com/questions/22014/does-mysql-have-a-limitation-of-one-index-per-query
Variables
JS
var colors = “green”;
PHP
$colors = “green”;
==========================================
Array
JS
var colors ;
colors = [‘white’,’black’,’brown’];
colors[0];
OR
var colors = Array(‘white’,’black’,’brown’);
colors.item(0);
==========================================
Object
JS:
var hotel = {
name: ‘Super8’,
rooms: 40,
booked: 25
checkAvailability = function(){
return this.rooms – this.booked;
}
};
var hotelName = hotel.name;
var roomsFree = hotel.checkAvailability();
1) What is the difference between primary key and unique key ?
both primary and unique key uniquely identifies each row in table but there are some subtle difference between them. here are some of them :
1) Unique key in a table can be null, at-least one but primary key can not be null in any table in relation database like MySQL , Oracle etc.
2) Primary key can be combination of more than one unique keys in same table.
3) There can be only one primary key per table in relation database e.g. MySQL, Oracle or Sybase but there can be more than one unique key per table.
4) Unique key is represented using unique constraint while primary key is created using primary key constraint in any table and it’s automatically gets unique constraint.
5) Many database engine automatically puts clustered index on primary key and since you can only have one clustered index per table, its not available to any other unique key at same time.
2) What is a clustered index?
With a clustered index the rows are stored physically on the disk in the same order as the index. There can therefore be only one clustered index.
With a non clustered index there is a second list that has pointers to the physical rows. You can have many non clustered indexes, although each new index will increase the time it takes to write new records.
A clustered index means you are telling the database to store close values actually close to one another on the disk. This has the benefit of rapid scan / retrieval of records falling into some range of clustered index values.
Clustered Index
Only one per table.
Faster to read than non clustered as data is physically stored in index order.
Nonclustered Index
Can be used many times per table.
Quicker for insert and update operations than a clustered index.
3) How many triggers are possible in MySQL?
Answer : There are only six triggers are allowed to use in MySQL database and they are.
Before Insert
After Insert
Before Update
After Update
Before Delete
After Delete
4) You wrote a search engine that should retrieve 10 results at a time, but at the same time you’d like to know how many rows there’re total. How do you display that to the user?
SELECT SQL_CALC_FOUND_ROWS page_title FROM web_pages LIMIT 1,10; SELECT FOUND_ROWS(); The second query (not that COUNT() is never used) will tell you how many results there’re total, so you can display a phrase “Found 13,450,600 results, displaying 1-10”. Note that FOUND_ROWS does not pay attention to the LIMITs you specified and always returns the total number of rows affected by query.
5) How do you find out which auto increment was assigned on the last insert?
SELECT LAST_INSERT_ID() will return the last value assigned by the auto_increment function.