Javascript Questions Set4

Global Variables:
The var keyword is used to declare a local variable or object, while omitting the var keyword creates a global variable.

Types in Javascript:
JavaScript is a loosely-typed language (some call this weakly typed); this means that no type declarations are required when variables are created. Strings and numbers can be intermixed with no worries. JavaScript is smart, so it easily determines what the type should be. The types supported in JavaScript are: Number, String, Boolean, Function, Object, Null, and Undefined.

What does “this” refer to in Javascript?
In JavaScript the this is a context-pointer and not an object pointer. It gives you the top-most context that is placed on the stack.

Every line of JavaScript code is run in an “execution context.” The JavaScript runtime environment maintains a stack of these contexts, and the top execution context on this stack is the one that’s actively running.

There are three types of executable code: Global code, function code, and eval code. Roughly speaking, global code is code at the top level of your program that’s not inside any functions, function code is code that’s inside the body of a function, and eval code is global code evaluated by a call to eval.

The object that this refers to is redetermined every time control enters a new execution context and remains fixed until control shifts to a different context. The value of this is dependent upon two things: The type of code being executed (i.e., global, function, or eval) and the caller of that code.

That’s it! You can figure out what object this refers to by following a few simple rules:

– By default, this refers to the global object.
– When a function is called as a property on a parent object, this refers to the parent object inside that function.
– When a function is called with the new operator, this refers to the newly created object inside that function.
– When a function is called using call or apply, this refers to the first argument passed to call or apply. If the first argument is null or not an object, this refers to the global object.

Why to use call and apply instead of calling a function directly ?
You use call or apply when you want to pass a different this value to the function. In essence, this means that you want to execute a function as if it were a method of a particular object. The only difference between the two is that call expects parameters separated by commas, while apply expects parameters in an array.

Sorting Algorithms

1) Merge Sort

– O(n log n) comparison based sorting algorithm (best case, worst case, avg case)
– Produces a stable sort (implementation preserves the input order of equal elements in the sorted output)
– Top Down Implementation and Bottom Up Implementation
– Is more efficient at handling slow-to-access sequential media. Merge sort is often the best choice for sorting a linked list
– Uses more memory (2n locations)
– Can be parallelized

2) Quick Sort
– Avg O(n log n) , Worst Case O(n ^ 2)
– Is comparison based sorting
– In place partioning algorithm and hence uses O(log n) additional space
– Can be parallelized (like mergesort)
– Depending on implementation may or may not be a stable sort
– Low memory required
– Useful in cases where data is in RAM, not useful in cases where data lives on disk (In that case use merge sort)
– usually faster than mergesort
– The worst-case running time for quicksort is O(n2), which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk

QuickSort is more popular than Merge Sort because it:

Is in-place (MergeSort requires tons of extra memory).
Has a small hidden constant

3) Heap Sort

– Heapsort is part of the selection sort family. Is a comparison based sorting algo.
– it has the advantage of a more favorable worst-case O(n log n) runtime
– Heapsort is an in-place algorithm, but it is not a stable sort
– Quicksort is typically faster than Heapsort, but the worst-case running time for quicksort is O(n2), which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk
– Merge sort requires ?(n) auxiliary space, but heapsort requires only a constant amount.

Merge sort has several advantages over heapsort:
Merge sort on arrays has considerably better data cache performance, often outperforming heapsort on modern desktop computers because merge sort frequently accesses contiguous memory locations (good locality of reference); heapsort references are spread throughout the heap.
Heapsort is not a stable sort; merge sort is stable.
Merge sort parallelizes well and can achieve close to linear speedup with a trivial implementation; heapsort is not an obvious candidate for a parallel algorithm.
4) Intro Sort

Must Know Algorithms

As per Cracking the Coding Interview:

Data Structures
Linked Lists
Binary Trees
Tries
Stacks
Queues
Vectors/ArrayLists
Hash Tables

Algorithms
Breadth First Search
Depth First Search
Binary Search
Merge Sort
Quick Sort
Tree Insert/Find/etc

Concepts
Bit Manipulation
Singleton Design Pattern
Factory Design Pattern
Memory (Stack vs Heap)
Recursion
Big-O Time

Design Patterns

In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn’t a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

There are three basic kinds of design patterns:

  • creational
  • structural
  • behavioral

Creational patterns provide instantiation mechanisms, making it easier to create objects in a way that suits the situation.

Structural patterns generally deal with relationships between entities, making it easier for these entities to work together.

Behavioral patterns are used in communications between entities and make it easier and more flexible for these entities to communicate.

References :
http://sourcemaking.com/design_patterns/factory_method

https://refactoring.guru/design-patterns/

 

================================ BEHAVIORAL =================================

Software Architect Interview Questions

What do you understand from the word Architecture? What is meant by software Architecture?

What kind of Software Architecture have you designed till now? Can you explain with the help of commands and patterns?

Which kind of design patterns have you used while building software architecture?

Explain pattern? What is design pattern and factory pattern? Where can these be used?

What is the use of builder pattern? Why it is so important in Software Architecture?

What is shallow copy and deep copy in prototype pattern? Describe: singleton and command pattern?

Numerate the advantages and disadvantages of composition and inheritance in Software Architecture?

When do you use abstract class and what are the conditions to use interfaces?

Explain the difference between object oriented, interface oriented and aspect oriented design and programming?

In which case do you use interface oriented and aspect oriented designs?

Difference between Object Oriented Programming and Component Oriented Programming
https://www.safaribooksonline.com/library/view/programming-net-components/0596102070/ch01s02.html

Finding if a string occurs in another string

Options:
1) strpos
2) strstr

If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead of strstr.

For case insensitive queries, use stripos and stristr

strstr: Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack. Has option in new version of PHP to return part of string before needle also. Returns false if not found.

strpos: Find the numeric position of the first occurrence of needle in the haystack string.
Returns false if not found.

Different types of errors in PHP

There are three basic types of runtime errors in PHP:

1. Notices: These are small, non-critical errors that PHP encounters while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although the default behavior can be changed.

2. Warnings: Warnings are more severe errors like attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

3. Fatal errors: These are critical errors – for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP’s default behavior is to display them to the user when they take place.

Can be changed by
a) php statement error_reporting — Sets which PHP errors are reported
b) php.ini : error_reporting = E_ALL