Saturday, October 8, 2016

Verify if the given string is valid parentheses expression in c#

You are given a string with series for parentheses. The parentheses could be only of 3 opening parentheses {, (, [ and 3 closing parentheses }, ), ]. You need to figure out if the given input are valid expression or not. Valid mean if there is proper closing and opening parentheses.

Other details and algorithm can be found here. Below is the c# implementation.

http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/

Saturday, September 10, 2016

C# Implementation of Heap Sort

The code create the maxheap of the given array of size N and then swap the 0th index (max value) with the last index (N-1) and then again create maxheap with reduced array of size (N-1).

Continue this process unless complete array is sorted.


Thursday, March 17, 2016

Check if WMI class exist on machine through powershell

I was debugging a powershell which throw exception when it tries to GET the WMI class property of the class which does not exist on OS. I found that there are not simple example which explains the code to check the existence of wmi class and then query it property. Therefore i found a code snippet which works perfect for such scenario which check if class exists or not and then you can further query its property.


Example

if(Get-WmiObject -List | where { $_.Name -eq "Win32_PerfRawData_Counters_HyperVDynamicMemoryIntegrationService"})
{
          write-output "class exist"
}
else
{
          write-output "class do not exist"
}


I am checking the existence of wmi class "Win32_PerfRawData_Counters_HyperVDynamicMemoryIntegrationService".

On win2008 r2 / window7 and prev OS it does not exists therefore the code goes in else part.

On latest OS the if block get executed.

Wednesday, September 30, 2015

Rest vs Soap

I was reading the difference between REST and SOAP api and figured out why world is moving towards REST apis. I tried to write down few difference between them. I have also given links to few references as well.

http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

Rest(Representational State Transfer )
Soap(Simple Object Access Protocol)
REST describes a set of architectural principles by which data can be transmitted over a standardized interface (such as HTTP)
SOAP defines a standard communication protocol.

The Web Services Description Language (WSDL) contains and describes the common set of rules to define the messages, bindings, operations and location of the Web service. WSDL is a sort of formal contract to define the interface that the Web service offers.
The W3C Technical Architecture Group (TAG) developed the REST architectural style in parallel with HTTP 1.1 of 1996-1999.
Microsoft introduced it.
REST is focused on accessing named resources through a single consistent interface
SOAP brings its own protocol and focuses on exposing pieces of application logic (not data) as services
Different data format XML/Json/CSV are supported.
SOAP supports XML
Rest reads can be cached
SOAP read cannot be cached.
While REST supports transactions, it isn’t as comprehensive and isn’t ACID compliant
WS-Security which adds some enterprise security features

Need ACID Transactions over a service, you’re going to need SOAP

SOAP has successful/retry logic built in and provides end-to-end reliability
Implementation is simpler. Beneficial for public apis. Easier to document and easier for end user/browser as well.

You can simply use browser for make a GET request. For other HTTP VERBS such as POST/PUT you can use various browser plugin and fiddler.

Advanced rest client chrome extension is my favorite.
SOAP is not as easier to code as compare to REST. Every time I ask my colleague that we need to implement a 3rd party API which is in SOAP he gives me weird look.
The RESTful Web services are completely stateless.


Session state is therefore kept entirely on the client. This constraint induces the properties of visibility, reliability, and scalability.

Visibility is improved because a monitoring system does not have to look beyond a single request datum in order to determine the full nature of the request.

Reliability is improved because it eases the task of recovering from partial failures.

Scalability is improved because not having to store state between requests allows the server component to quickly free resources, and further simplifies implementation because the server doesn’t have to manage resource usage across requests. Like most architectural choices, the stateless constraint reflects a design trade-off. The disadvantage is that it may decrease network performance by increasing the repetitive data (per-interaction overhead) sent in a series of requests, since that data cannot be left on the server in a shared context. In addition, placing the application state on the client-side reduces the server’s control over consistent application behavior, since the application becomes dependent on the correct implementation of semantics across multiple client versions


SOAP are stateful.


If a user attempts to upload something to a mobile app (say, an image to Instagram) and loses reception, REST allows the process to be retried without major interruption, once the user regains cell service.
If the SOAP service is interrupted it would require more initialization and state code.

The client context is stored on the server between requests which make initialization time consuming.

Monday, May 27, 2013

Stack and Queue Algorithm Questions


  1. Use single array to implement 3 stack.
  2. Implement stack class. Write push , pop and peek function. Write a function MIN which return minimum value in O(1).
  3. Implement queue using 2 stacks.


Sorting and Searching Algorithm Questions


  1. Given two sorted array A and B. A is large enough to hold B. Merge the arrays in sorted order.
  2. Write a function which sort the array of string such that 2 anagram are together.
  3. A array is sorted in increasing order and rotated many times. Find a number N in given array.
  4. You have 10 GB of file with each line having 1 string.Sort the file.
  5. Given sorted array of string. The empty string is inserted between each string. Find a given string in array.
  6. Given a matrix MxN which is sorted by row and column in increasing order. Find given element .

Tree and Graph Algorithm Questions


  1. Implement a function to check if binary tree is balanced. A balanced tree is whose left and right sub-tree height do not differ more than 1.
  2. Given a directed graph. Write algo to find route between 2 nodes.
  3. Given a sorted increasing order arrray. Create binary search tree with minimum height.
  4. Given a binary tree. Create a link list of all nodes at each depth.
  5. Write function which checks whether given binary tree is binary search tree.
  6. Write algo for next node "in order successor" of a given node in binary search tree.
  7. Write function of find common ancestor of 2 nodes. This may not be BST and avoid using other data structure.
  8. Given 2 very large binay tree. Find one tree is sub-tree of another at node N. So that if we cut tree at node N then both the tree are identical.
  9. Given the binary tree. Print all the paths whose sum is equal to gicen value. Path can start and end anywhere.