Friday, January 6, 2012

The Key Differences Between SAN, DAS, and NAS

  • Direct-Attached Storage(DAS)

The most common form of server storage today is still direct attached storage.
The disks may be internal to the server or they may be in an array that is connected directly to the server
Either way, the storage can be accessed only through that server.
An application server will have its own storage; the next application server will have its own storage;
and the file and print servers will each have their own storage



  • Storage Area Networks(SAN)

A SAN allows more than one application server to share storage.
Data is stored at a block level and can therefore be accessed by an application, not directly by clients
The physical elements of the SAN (servers, switches, storage arrays, etc.) are typically connected with Fibre-Channel – an interconnect technology that permits high-performance resource sharing
Storage can be added without disrupting the applications, and different types of storage can be added to the pool.


A storage area network (SAN) is a dedicated network that provides access to consolidated, block level data storage. SANs are primarily used to make storage devices, such as disk arrays, tape libraries, and optical jukeboxes, accessible to servers so that the devices appear like locally attached devices to the operating system. A SAN typically has its own network of storage devices that are generally not accessible through the local area network by other devices
In storage networking terminology, a Storage Area Network (SAN) is a high-speed subnetwork of shared storage devices. A storage device is a machine that contains nothing but a disk or disks for storing data.

A SAN's architecture works in a way that makes all storage devices available to all servers on a LAN or WAN. As more storage devices are added to a SAN, they too will be accessible from any server in the larger network. In this case, the server merely acts as a pathway between the end user and the stored data.

Because stored data does not reside directly on any of a network's servers, server power is utilized for business applications, and network capacity is released to the end user.




  • Network Attached Storage(NAS)

A NAS appliance is a simplified form of file server; it is optimized for file sharing in an organization.
Authorized clients can see folders and files on the NAS device just as they can on their local hard drive
NAS devices are frequently used to consolidate file services.
To prevent the proliferation of file servers, a single NAS appliance can replace many regular file servers, simplifying management and reducing cost and workload for  the systems administrator.
NAS appliances are also multiprotocol, which means that they can share files among clients using Windows® and UNIX®-based operating systems.

Reference:
http://smbtechconnect.securelement.com/2008/03/key-differences-between-san-das-and-nas.html


  • An optical jukebox is a robotic data storage device that can automatically load and unload optical discs, such as Compact Disc, DVD, Ultra Density Optical or Blu-ray disc and can provide terabytes (TB) and petabytes (PB) of tertiary storage.

https://en.wikipedia.org/wiki/Optical_jukebox

spring interview questions


  • What is IOC (or Dependency Injection)?


The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created
You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file


  • What is Spring ?

Spring is an open source framework created to address the complexity of enterprise application development.
Spring is a lightweight inversion of control and aspect-oriented container framework.



  • What are the advantages of Spring framework?

Spring has layered architecture.
Use what you need and leave you don't need now.
Spring Enables POJO Programming.
 POJO programming enables continuous integration and testability.
Dependency Injection and Inversion of Control Simplifies JDBC


  • What are features of Spring ?
Inversion of control (IOC):



Loose coupling is achieved in spring using the technique Inversion of Control.
The objects give their dependencies instead of creating or looking for dependent objects

Aspect oriented (AOP):
Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.

MVC Framework:
Spring comes with MVC web application framework, built on core Spring functionality.
This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI.
But other frameworks can be easily used instead of Spring MVC Framework.

JDBC Exception Handling:
The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy.
Integration with Hibernate, JDO, and iBATIS:
Spring provides best Integration services with Hibernate, JDO and iBATIS



  • What are the different modules in Spring framework?

The Core container module
Application context module
AOP module (Aspect Oriented Programming)
JDBC abstraction and DAO module
O/R mapping integration module (Object/Relational)
Web module
MVC framework module

Reference:
http://www.developersbook.com/spring/interview-questions/spring-interview-questions-faqs.php
http://www.javabeat.net/articles/103-spring-framework-interview-questions-1.html




  • Model-view-controller framework

The Spring Framework features its own MVC web application framework, which wasn't originally planned. The Spring developers decided to write their own web framework as a reaction to what they perceived as the poor design of the (then) popular Jakarta Struts web framework,
Like Struts, Spring MVC is a request-based framework
MVC paves the way for cleaner front end code. All interfaces are tightly coupled to the Servlet API.
This tight coupling to the Servlet API is seen by some as a failure on the part of the Spring developers to offer a high-level abstraction for web-based applications[citation needed].
However, this coupling makes sure that the features of the Servlet API remain available to developers while offering a high abstraction framework to ease working with said API.
http://en.wikipedia.org/wiki/Spring_Framework#Model-view-controller_framework



  • With desktop Java apps, you can have a single piece of 

code that instantiates the container and gets beans
• I.e., driver class that calls instantiates
ClassPathXmlApplicationContext and calls getBean
– With Web apps, each servlet wants access to beans
• But you want to instantiate container once only


bean scopes
– Standard Spring supports singleton and prototypeStandard Spring supports singleton and prototype
– Web apps also want request, session, and application


Regular Web Apps
Put bean definitions in WEB-INB/applicationContext.xml
• request and session scope now supported• request and session scope now supported
Declare two listeners in web.xml
• Container will be instantiated when app is loaded

JSF Apps
Same approach for JAR files, bean defn file, and listenerspp , ,
– Declare variable-resolver in faces-config.xml
Can declare beans in applicationContext or faces-config



applicationContext.xml
– Put “empty” file (with header and py (
<beans..></beans> only) in WEB-INF
• Unlike in desktop apps, the filename matters. The
standard loading utility assumes that name and location

/WEB-INF/applicationContext.xml
• If you want to change this default name/location, set a
context param called contextConfigLocation to override it


ContextLoaderListener
– This listener runs when the app is firs
instantiates the ApplicationContext (f
WEB-INF/applicationContext.xml) and places aWEB INF/applicationContext.xml) an
reference to it in the ServletContext
You can retrieve this reference with the static
tR i dW bA li ti C t t th d fgetRequiredWebApplicationContext method of
WebApplicationContextUtils


RequestContextListener
– This listener is needed if you declare any of your beans to
be request-scoped or session-scoped
IWb itdfth lSi f• I.e., Web scopes instead of the usual Spring scopes of
singleton or prototype


Not good to call getBean
– It would be technically legal to get the
ApplicationContext and call getBean explicitly (probably
from the backing bean’s action controller method). Butfrom the backing bean s action controller method). But
this is a bad idea since JSF is geared around declaring
beans in config files only



JSF already supports dependency injection
– The managed-property element lets you insert other beans
inside newly created ones.inside newly created ones.
• The only trick is to be able to refer to Spring beans



Use DelegatingVariableResolver
– Declare in faces-config.xml. Now, whenever JSF sees a
bean name, it uses JSF rules first, then Spring rules next.



Regular Web Apps
• Two JAR files
– Put Spring JAR files in WEB-INF/lib
• Bean definition file
PliiC liWEB INF– Put applicationContext.xml in WEB-INF
• Scopes now include request and session in addition to
singleton and prototype
• Listeners
– Two listener definitions in web.xml
G tti b• Getting beans
– Access ApplicationContext with static
getRequiredWebApplicationContext method ofgetRequiredWebApplicationContext method of
WebApplicationContextUtils
– Call getBean normally



JSF-Based Apps
• Basic setup
– Start with same setup as regular Web apps
– Use normal JSF definition of FacesServlet in web.xml
faces config xml• faces-config.xml
– Declare DelegatingVariableResolver
• Option 1• Option 1
– Declare Spring beans in applicationContext.xml
– Declare backing beans in faces-config.xmlg g
• Refer to Spring beans with managed-bean-property
• Option 2
– Declare all beans in applicationContext.xml
• Refer to other beans with ref and normal Spring syntax



Gets the Spring bean called sampleLookupService
and passes it to the setLookupService method of the
JSF backing bean called formBean (i.e., injects it
into the lookupService property).
into the lookupService property).


03-Spring-in-Web-Apps.pdf
http://courses.coreservlets.com/Course-Materials/spring.html

static scoping(lexical) vs dynamic scoping

  • ChatGPT

Static Scoping vs. Dynamic Scoping

Static Scoping (Lexical Scoping) and Dynamic Scoping are two different methods used to determine the scope and binding of variables in programming languages. Here's a detailed explanation with examples:


1. Static Scoping (Lexical Scoping)

Definition: In static scoping, the scope of a variable is determined by its position in the source code. The variable's binding is fixed at compile-time based on the program's structure.

Characteristics:

  • Variable references are resolved based on the program's text structure.
  • The scope of a variable is determined by the block or function where it is defined.
  • A variable can be accessed only within the block or function where it is declared, and this relationship does not change regardless of the program's execution.

int globalVar = 1;

void foo() {
    int localVar = 2;
    print(globalVar); // prints 1
    print(localVar);  // prints 2
}

void bar() {
    print(globalVar); // prints 1
    // print(localVar); // Error: localVar not defined in this scope
}

int main() {
    foo();
    bar();
}


In this example:

  • globalVar is accessible throughout the program because it is declared in the global scope.
  • localVar is only accessible within foo() because it is declared within foo()'s local scope.

2. Dynamic Scoping

Definition: In dynamic scoping, the scope of a variable is determined by the call stack at runtime. The binding of a variable is based on the calling sequence of functions, not the structure of the code.


Characteristics:

  • Variable references are resolved based on the execution call stack.
  • The scope of a variable is determined dynamically, depending on the function call hierarchy at runtime.
  • Variables are accessible in functions that are called within the context where the variable was defined.
int globalVar = 1;

void foo() {
    int localVar = 2;
    print(globalVar); // prints 1 (globalVar is accessible)
    print(localVar);  // prints 2
}

void bar() {
    print(globalVar); // prints 2 (globalVar is from foo's scope in dynamic scoping)
}

int main() {
    foo();
    bar();
}

In this example:

  • globalVar is set to 1 globally.
  • When bar() is called after foo(), dynamic scoping would cause globalVar to print 2 if bar() could access globalVar set in foo().

Comparison

  • Static Scoping:

    • Variable scope is determined by the position of the variable in the source code.
    • More predictable and easier to understand because the scope does not change dynamically.
    • Example languages: C, Java, Python (mostly).
  • Dynamic Scoping:

    • Variable scope is determined by the runtime call stack.
    • Less predictable because the scope can change depending on function calls.
    • Example languages: Some Lisp dialects (like older versions of Emacs Lisp).
Summary: Static scoping relies on the structure of the code to determine variable scope, making it more predictable. Dynamic scoping relies on the call stack and the sequence of function calls, making it more flexible but potentially less predictable.
  • ChatGPT

To determine the output of the given pseudocode under both static (lexical) scoping and dynamic scoping, we need to understand how variable scopes are managed in each case.

Pseudocode Analysis:

int i
main ()
{
    i = 3
    S ()
    R ()
}

void S ()
{
    print i // prints the value of i on the current line of output
    print " " // prints a blank space on the current line of output
}

void R ()
{
    int i
    i = 2
    S ()
}


Static Scoping (Lexical Scoping)

In static scoping, the scope of a variable is determined by its physical location in the code.

  1. In main: i is set to 3. The function S() is called first.

    • S() prints the value of i from main, which is 3.
    • S() then prints a blank space.
  2. In R: A new local i is created and set to 2.

    • When S() is called within R(), it refers to the i from main due to static scoping.
    • So S() prints the i from main, which is still 3, and then a blank space.

Static Scoping Output:

3 (from main)  // `S()` prints `3` from `main`

(Blank space)  // `S()` prints a space

3 (from main)  // `S()` prints `3` from `main` again in `R()`


Output: 3 3


Dynamic Scoping

In dynamic scoping, the scope of a variable is determined by the call stack at runtime.

  1. In main: i is set to 3. The function S() is called.

    • S() prints the i from main, which is 3.
    • S() then prints a blank space.
  2. In R: A new local i is created and set to 2.

    • When S() is called within R(), it will print the i from R(), which is 2, because dynamic scoping looks up the variable from the most recent context in the call stack.

Dynamic Scoping Output:


3 (from main)  // `S()` prints `3` from `main`

(Blank space)  // `S()` prints a space

2 (from `R`)   // `S()` prints `2` from `R()`


Output: 3 2

Summary:

  • Static Scoping Output: 3 3
  • Dynamic Scoping Output: 3 2

So the answers for the options provided are:

  • Static Scoping: Option D) 3 3 3 2
  • Dynamic Scoping: Option C) 3 3 2 2

  • Gemini

Static vs. Dynamic Scoping: Understanding the Output
Understanding Static and Dynamic Scoping

Before we analyze the output, let's clarify the two scoping methods:

    Static Scoping (Lexical Scoping): The scope of a variable is determined by its position in the code, not by the calling function. This is the most common scoping method in modern programming languages.
    Dynamic Scoping: The scope of a variable is determined by the calling sequence of functions at runtime.

Analyzing the Pseudocode

Given Pseudocode:

int i
main ()
{
i = 3
S ()
R ()
}
void S ()
{
print i // prints the value of i on the current line of output
print " " // prints a blank space on the current line of output
}
void R ()
{
int i
i = 2
S  

 ()
}

Static Scoping:

    The i in function S refers to the global i which is initialized to 3.
    The i in function R is a local variable and hides the global i.
    The output will be: 3 3 2

Dynamic Scoping:

    The i in function S will refer to the most recently defined i.
    When S is called from R, the i in R is the most recent, so it will print 2.
    The output will be: 3 2 3

Answer:

    Static Scoping: (C) 3 3 2 3
    Dynamic Scoping: (B) 3 3 2 2

Explanation:

    The first 3 is printed from the main function.
    The second 3 is printed from function S, referring to the global i in static scoping, and the local i in R in dynamic scoping.
    The 2 is printed from function R.
    The last 3 is printed from function S again, referring to the global i in static scoping.

This example demonstrates the difference between static and dynamic scoping. Static scoping is more predictable as it relies on the code structure, while dynamic scoping depends on the function call stack at runtime, which can lead to unexpected results.

Statically typed languages check types at compile time
The programmer ensures that parameter types are specified and the compiler ensures the programmers wishes will be followed.

In a dynamically typed language (like ruby, javascript, etc), types are not checked until execution
If an expression evaluates, then the type-checking worked. If not, it blows up to your error handling or the user

Static Scoping – Variables can be bound at compile time without regards to calling code.

Dynamic Scoping – Variable binding (context) can only be determined at the moment code is executed.



Reference:
http://hoolihan.net/blog-tim/2009/02/17/static-vs-dynamic-scope/
http://en.wikipedia.org/wiki/Scope_%28programming%29




  • First, Lexical Scope (also called Static Scope), in C-like syntax:

void fun()
{
    int x = 5;

    void fun2()
    {
        printf("%d", x);
    }
}
Every inner level can access its outer levels.
There is another way, called Dynamic Scope used by first implementation of Lisp, again in C-like Syntax:
void fun()
{
    printf("%d", x);
}

void dummy1()
{
    int x = 5;

    fun();
}

void dummy2()
{
    int x = 10;

    fun();
}
Here fun can either access x in dummy1 or dummy2, or any x in any function that call fun with xdeclared in it.
dummy1();
will print 5
dummy2();
will print 10

The first one is called static because it can be deduced at compile-time, the second is called dynamic because the outer scope is dynamic and depends on the chain call of the functions.
Dynamic scoping is like passing references of all variables to the called function.
stackoverflow

stack vs heap

how the memory of the computer is organized for a running program
When a program is loaded into memory, it is organized into three areas of memory, called segments:
the text segment,
stack segment,
heap segment.

The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides
This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.

The stack is where memory is allocated for automatic variables within functions.

The heap segment provides more stable storage of data for a program;
memory allocated in the heap remains in existence for the duration of a program.
Therefore, global variables (storage class external), and static variables are allocated on the heap.


The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored
The heap is the section of computer memory where all the variables created or initialized at runtime are stored

computer memory, it is organized into three segments:

text (code) segment
stack segment
heap segment


The text segment (often called code segment) is where the compiled code of the program itself resides. When you open some EXE file in Notepad, you can see that it includes a lot of "Gibberish" language, something that is not readable to human. It is the machine code, the computer representation of the program instructions.


Heap and stack in Java
When you create an object using the new operator, for example myobj = new Object();, it allocates memory for the myobj object on the heap.
The stack memory space is used when you declare automatic variables.

for example String myString;, it is a reference to an object so it will be created using new and hence it will be placed on the heap

Reference:
http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html
http://www.maxi-pedia.com/what+is+heap+and+stack





  • Stack memory

-Size required determined at compile-time, so no need at run time to determine amount of memory to allocate at run time. This is obviously a performance advantage
In contrast, the size of objects allocated on the stack, needs to be known at compile time

-Automatic "clean-up"
your program takes care of stack memory (allocating it & cleaning it up)

-The most important thing about the stack is that you don't need to think much about it. Your program manages it.

-The size of the stack is usually quite small because it's mostly used for local variables (variables that go out of scope pretty soon after they're finished with). The size of the stack is set at compile time and is small enough to be available on any computer, no matter how little RAM it's got

-Stack is where variables declared before runtime are stored.





  • Heap memory

-It's also used very frequently to allocate memory for things whose size is not known until run time. For example, if you had to open a file and read in the data, you don't know how much data exists until run time

-The size of objects allocated on the heap is often not known to the compiler - therefore the programmer must allocate and release the memory specifically.

-If a particular computer has a lot of RAM, why not make use of it?
Therefore the heap consists of memory that can be allocated at run time

-Heap memory is most often used for 'large' things (e.g. the contents of a file) - or for things that need to 'stay around' in memory so that different functions can share the data (in other words, for NON-local variables).

-Heap is where variables created or initialized at runtime are stored.

-On the other hand, heap is an area of memory used for dynamic memory allocation


http://hep.ph.liv.ac.uk/~gwilliam/cppcourse/




  • When you look at your computer memory, it is organized into three segments:

?text (code) segment
?stack segment
?heap segment



The text segment (often called code segment) is where the compiled code of the program itself resides. When you open some EXE file in Notepad, you can see that it includes a lot of "Gibberish" language, something that is not readable to human. It is the machine code, the computer representation of the program instructions. This includes all user defined as well as system functions.




  • What is stack?

The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated for automatic variables within functions.

Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.






  • What is heap?

On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.

The stack is much faster than the heap but also smaller and more expensive.









Reverse a String

A typical programming interview question is “reverse a string, in place”. if you understand pointers, the solution is simple. even if you don’t, it can be accomplished using array indices


to reverse the string word by word, in place. for example if our string is “the house is blue”, the return value would be “blue is house the”. the words are reversed, but the letters are still in order


initial: the house is blue
reverse: eulb si esuoh eht
wanted : blue is house the

the solution can be attained by first reversing the string normally, and then just reversing each word.

Reference:
http://www.techinterview.org/post/526374214/reverse-a-string





  • 3-ways to reverse a string in java



First Solution: StringBuffer
The easiest way to reverse a String in Java is by using an instance of the StringBuffer class as it already contains a reverse() method.


Second Solution: Reverse For Loop
You can also reverse a String by traversing it from the end in a traditional for loop. For this approach you can either use a char array which would be somewhat more efficient than by creating a large String pool as a result of continuous concatenation to a String variable.

Third Solution: Recursion

http://www.brilliantsheep.com/3-ways-to-reverse-a-string-in-java/

algorithms & data structures

Understanding how to choose the best algorithm or data structure for a particular task is one of the keys to writing high-performance software
If you start with the wrong algorithm, all the micro-level tuning in the world won't produce optimal results.


  • What is a data structure? What are the types of data structures?

Data structures are used to store data in a computer in an organized fashion.


  • Different types of data structures are:


  1. Arrays: A set of homogeneous values
  2. Records: A set of fields, where each field consists of data belongs to one data type.
  3. Stack- Works in first in last out order. The element inserted first in stack is removed last.
  4. Queue- First in First out order. The element inserted first is removed first.
  5. Linked list- Stored data in a linear fashion.
  6. Trees- Stores data in a non linear fashion with one root node and sub nodes.A data structure where the data is organized in a hierarchical structure.This type of data structure follows the sorted order of insertion, deletion and modification of data items




  • Define a linear and non linear data structure

Linear data
Linked list is an example of linear data storage or structure.
Linked list stores data in an organized a linear fashion. They store data in the form of a list.
A linear data structure traverses the data elements sequentially, in which only one data element can directly be reached.
Ex: Arrays, Linked Lists

Non Linear data structure
Tree data structure is an example of a non linear data structure.
A tree has one node called as root node that is the starting point that holds data and links to other nodes.
Every data item is attached to several other data items in a way that is specific for reflecting relationships.
The data items are not arranged in a sequential structure.
Ex: Trees, Graphs

data structure is a way of organizing data that considers how items are stored and their relationship between them


Reference:
http://java.sun.com/docs/books/performance/1st_edition/html/JPAlgorithms.fm.html
http://www.java2s.com/Code/Java/Collections-Data-Structure/CatalogCollections-Data-Structure.htm
http://www.careerride.com/Data-Structure-Interview-Questions.aspx
http://globalguideline.com/interview_questions/Questions.php?sc=Data_Structures_Interview_Questions_and_Answers_&page=1

B+ tree

In computer science, a B+ tree or B plus tree is a type of tree which represents sorted data in a way that allows for efficient insertion, retrieval and removal of records, each of which is identified by a key. It is a dynamic, multilevel index, with maximum and minimum bounds on the number of keys in each index segment (usually called a "block" or "node"). In a B+ tree, in contrast to a B-tree, all records are stored at the leaf level of the tree; only keys are stored in interior nodes.

Reference:
http://en.wikipedia.org/wiki/B%2B_tree



  • Consider the following B+ tree with degree d = 2,
which means each node except the root has between 2 and 4 keys, as shown in Figure 1. We label
each node as A0,B0,B1,C0,C1 ... so that you can redraw the tree easily

(1)Different applications with different types of queries may require different indexes on a database
in order to have high efficiency. What type of queries, which your application needs a lot, will
make you choose a B+ tree index over a hash table index? Can you give an example of this type
of queries.(2 points)

Solution When an application needs a lot of range queries, a B+ tree index is preferred. For
example, find all the data entries between 20 and 22

(2)Show the B+ tree that would result from deleting the data entry with key 79 from the original
tree. You MUST redraw the whole tree in your result. But you can use just the labels to denote
those nodes that are unchanged. An example of deleting the data entry with key 6 from the original
tree is shown in Figure 2. If you add descriptions about how the B+ tree is changed, you may get
partial credits if your result drawing is not totally correct. (4 points)

(3) Show the B+ Tree that would result from adding the data entry with key 10 from the original
tree. You MUST redraw the whole tree in your result. But you can use just the labels to denote
those nodes that are unchanged. If you add descriptions about how the B+ tree is changed, you
may get partial credits if your result drawing is not totally correct. (4 points)


webcache.googleusercontent.com/search?q=cache:8vRupC7L9OYJ:https://wiki.engr.illinois.edu/download/attachments/227743489/CS411-F2011-Final-Sol.pdf%3Fversion%3D1%26modificationDate%3D1380470739000+&cd=3&hl=tr&ct=clnk&gl=tr&client=firefox-a