Wednesday, June 1, 2016

Today we discuss, taking snapshots with instances in OpenStack. A backup is different from a snapshot in that a backup can be used to restore instance to its state where as a snapshot is a point of time capture of an instance, its filesystem but not its memory. 
To take a live snapshot on Openstack, we can issue the following command: 
nova image-create <instance name or uuid> <name of new image> 
Openstack also provides snapshot APIs from the Images library which are as follows: 
GET /v2/images   
GET /v2/images/{image_id}   
And    
POST /v2/images   
eg 
curl -i -X POST -H "X-Auth-Token: $OS_AUTH_TOKEN" \            -H "Content-Type: application/json" \            -d '{"name": "Ubuntu 12.10", "tags": ["ubuntu", "12.10", "quantal"]}' \            $OS_IMAGE_URL/v2/images 
which gives an output such as: 
    {         "id": "7b97f37c-899d-44e8-aaa0-543edbc4eaad",         "name": "Ubuntu 12.10",         "status": "queued",         "visibility": "private",         "protected": false,         "tags": ["ubuntu", "12.10", "quantal"],         "created_at": "2012-08-14T00:46:48Z",         "updated_at": "2012-08-14T00:46:48Z",         "file": "/v2/images/7b97f37c-899d-44e8-aaa0-543edbc4eaad/file",         "self": "/v2/images/7b97f37c-899d-44e8-aaa0-543edbc4eaad",         "schema": "/v2/schemas/image"     } 

Upload and download of raw image data can be done with:    
PUT /v2/Images/{image_id}/file   
GET /v2/Images/{image_id}/file   

For example: 
curl -i -X PUT -H "X-Auth-Token: $OS_AUTH_TOKEN" \            -H "Content-Type: application/octet-stream" \            -d @/home/glance/ubuntu-12.10.qcow2 \            $OS_IMAGE_URL/v2/images/7b97f37c-899d-44e8-aaa0-543edbc4eaad/file 
and  
curl -i -X GET -H "X-Auth-Token: $OS_AUTH_TOKEN" \            $OS_IMAGE_URL/v2/images/7b97f37c-899d-44e8-aaa0-543edbc4eaad/file 

Notice that these APIs take an OAuth token for the administrator. In Openstack deployments, this may be very sensitive and not available for everyone’s consumption.  
Shape
Instead these APIs can be made private and the API can be wrapped and exposed internally where a different authentication takes place on behalf of the users. 
#codingexercise 
Find the nearest element in a Binary Search Tree
GetNearest(Node root, int val, ref target)
{
if (root == null) { target = null; return;}
if (root.data == val) {target = root; return; }
if (val < root.data){
if (root.left == null) {target  = root; return;}
GetNearest(root.left, val, target);
if (Math.abs(target.val - val) > Math.abs(root.val-val)){ target=root; return;}
}else{
if (root.right == null) {target  = root; return;}
GetNearest(root.right, val, target);
if (Math.abs(target.val - val) > Math.abs(root.val-val)){ target=root; return;}

}
}
return null;
}


Find the maximum sum root to leaf in a tree 
Void GetLeafMax ( node root, int sum,  ref int max, ref leaf) 
{ 
If (root == null) return; 
sum += root.data; 
If (root.left == null && root.right == null) 
{ 
If (sum > max){ 
    sum = max; 
    leaf = root; 
} 
} 
GetLeafmax (root.left, sum, max); 
GetLeafmax (root.right, sum, max); 
} 
Bool PrintPath(Node root, node leaf) 
{ 
If (root==null) return false; 
If (root == leaf || 
    PrintPath(Root.left, leaf) || 
    PrintPath(root.right, leaf)) 
{ 
Console.Write(root.data); 
Return true; 
} 
Return false; 
} 
Int maxSumPath(Node root) 
{ 
If (root == null) return 0; 
Node leaf; 
Int max = INT_MIN; 
GetLeafMax(node, 0, ref max, ref target_leaf);  
return max; 

} 

Given a non-negative number represented as an array of digits, plus one to the number.
void plusOne(ref List<int> digits, int position)
{
if (position  > digits.Length) return; 
if (position - 1 < 0) {// check INT_MIN underflow; digits.InsertAt(0, 1); return;}
if (digits[position] + 1 > 9)
   plusOne(ref digits, position - 1);
else
    digits[position] += 1;
}
This is a continuation of the series of coding exercises from previous posts to see a sample of the interview questions being asked.

Tuesday, May 31, 2016

This is a continuation of the series of coding exercises from previous posts to see a sample of the interview questions being asked.

#codingexercise
1. An Array of buildings is facing the sun. The heights of the buildings is given in an array, Find out which all buildings will see the sunset.
List<int > SeeSunsetBuildings(List<int> heights)
{
var ret = new List<int>();
if (heights.Count == 0) return ret;
int min = heights[0];
for (int i =1; i < heights.Count; i++)
{
if (heights[i] > min)
    ret.Add(heights[i])
}
return ret;
}
2. Print all nodes at a distance k from a given node
void printKDistanceDown(Node root, int k)
{
if (root == null || k < 0) return;
if (k == 0)
   Console.Write(root.data);
printKDistanceDown(root.left, k-1);
printKDistanceDown(root.right, k-1);
}

// returns distance of root from target
int PrintKDistanceNode(Node root, Node target, int k)
{
if (root == null) return -1;
if (root == target){printKDistanceDown(root, k); return 0;}
int dl = printKDistanceNode(root.left, target, k);
if (dl != -1)
{
   if (dl + 1 == k)
       Console.Write(root.data);
   else
       printKDistanceDown(root.right, k -dl-2);
   return 1 + dl;
}
int dr = printKDistanceDown(root.right, target, k);
if (dr != -1)
{
      if (dr +1 ==k)
         Console.Write(root.data);
      else
          printKDistanceDown(root.left, k-dr-2);
      return 1+dr;
}
return -1;
}

3. Count the decoding for a given digit string. Let say ‘A’ -> 1, B -> 2 and so on
 Eg : Input: digits[] = “123”
Output: 3  //”ABC”, “ LC” ,  “AW”

bool GetCode(string digits, ref stringbuilder candidate, ref int count)
{
if (digits.length == 0) { Console.Write(candidate.toString()); count += 1; return true; }
for (int i =0; i < 2 && i<=digits.length; i++)
{
  int code = int(digits.Substring(0,i+1);
  if (code >=1 && code <=26){
         candidate.add(lookup(code));
        if ( GetCode(digits.GetRange(i+1,digits.length-i-1), ref candidate,  ref count))
         return true;
}
return false;
}

Find the maximum sum root to leaf in a tree
Void getLeafMax ( node root, int sum,  ref int max)
{
If (root == null) return;
Sum += root.data;
If (root.left == null && root.right == null)
{
If sum > max
    sum = max;
}
Getleafmax (root.left, sum, max);
Getleafmax (root.right, sum, max);

Sunday, May 29, 2016

I haven't seen an example in node.js where an http response stream is piped to a downloadable file. So here it goes :


var path = require('path');

var mime = require('mime');



app.get('/download', function(req, res){



  var file = __dirname + '/upload-folder/dramaticpenguin.MOV';



  var filename = path.basename(file);

  var mimetype = mime.lookup(file);



  res.setHeader('Content-disposition', 'attachment; filename=' + filename);

  res.setHeader('Content-type', mimetype);



 var options = {

    host: 'sourceserver.com',

    port: 443,

    path: '/resource/',

    method: 'get',

    headers: {

    'Content-Type': 'application/octet-stream',

    'Authorization': 'Basic some_token',

    }

  };

  var httpreq = https.request(options, function (response) {

   // a response is a readable stream

   response.pipe(res);

   });

   httpreq.end();

});

#codingexercise
 A linked list has values sorted by absolute values. Sort the linked list by actual values
 void SortByActual(ref Node root)
{
if (root == null) return;
Node prev = root;
Node curr = prev.next;
while ( curr != null)
{
 if (curr.data < prev.data)
{
prev.next = curr.next;
Push(ref root, curr);
curr = prev;
}
else{
prev = curr;
}
curr = curr.next;
}

}
#programming interview questions
Determine cycles in graphs
Topological sort discussed in the previous blog post can help detect cycles. It is a linear ordering of all vertices along a horizontal line so that all the directed edges go from left to right. If the time taken to visit the next element is less than any of the predecessors then we have a cycle.
Bellman-Ford algorithm can work with negative-weight edges in the input graph. If there are no cycles, the algorithm takes gives the shortest paths and their weights. It relaxes each edge of the graph once and then determines cycle.
Bellman-Ford(G, w, s)
Initialize-single-source(G,s)
for i = 1 to number of vertices -1
    for each edge (u,v) belongs to edges E in G
          relax(u,v,w)
for each edge(u,v) belongs to edges E in G
     if (v.d  > u.d  + w(u,v))
         return False
return True

In a plane n points (X and Y) is given. How will you find out the maximum collinear points. Extend this algorithm for points (X, Y and Z) in 3D plane.
int Get MaxCollinear(List<Point> p)
{
var collinear = new List<Tuple<int,int,int>>();
for (int i =0; i < p.Count; i++)
   for (int j =0; j < p.Count; j++)
     for (int k =0; k<p.Count;k++)
    {
            if (p[i] ==p[j] || p[j] == p[k] || p[k] == p[i]) continue;
            if (collinear.contains(p[i],p[j],p[k])) continue;
            if (isCollinear(p[i],p[j],p[k]))  collinear.Add(p[i],p[j],p[k]);
     }
var ret = Merge(collinear);
return ret.count();
}

bool isCollinear(Point p, Point q, Point r)
{
if (q.x >= min(p.x,r.x) && q.x<= max(p.x,r.x)
    && q.y >= min(p.y,r.y) && q.y <= max(p.y,r.y)))
   return True;
return False;
}
List<Point> Merge(List<Tuple<int,int,int>> collinear)
{
// two points are common between entries
var max = new List<Point>()
{
for (int i =0 i < collinear.count; i++){
   var ret = new List<Point>();
   for (int j =0; j <collinear.count; j++)
    {
          if ( i != j  && collinear[i].Intersects(collinear[j]).Count > 2){
                       ret.add(collinear[i].items);
                       ret.add(collinear[j].items);
          }
    }
    if (ret.Count > max.Count)
          ret.CopyTo(ref max);
}
return max;
}

Saturday, May 28, 2016

#coding exercise
Write box stacking algorithm
int GetStackedBoxesCount(List<Tuple<int,int,int>> boxes)
{
int n = boxes.Length;
int best = new List<int>(n);
for (int i =1; i < n; i++)
for (int j = 0; j < i j++)
{
    if (box[i].first * box[i].second < box[j].first * box[j].second && best[j] + 1 > best[i])
          best[i] = best[j] + 1;
}
return best.max();
}

Minimum number of steps between start and end positions for a knight on a chessboard
void GetMinMoves(Point start, Point end, int count, ref int min)
{
 if ( start == end) { if (count < min), {min = count;} }
 var x = new List<int>() { 2, 1, -1, -2, -2, -1,  1,  2};
 var y = new List<int>() { 1, 2,  2,  1, -1, -2, -2, -1};
for (int k =0 ; k < 8; k ++)
{
   start.x += x[k];
   start.y += y[k];
   if (isValid(start))
       GetMinMoves(start, end, count + 1, ref min);
   start.x -= x[k];
   start.y -= y[k];
}
}

int GetWordSnake( char[,] board, Point start, int count, string candidate, Dictionary<string, string> d)
{
  for (int i =0; i< 4; i++)
  {
        start = update(start, i);
        candidate.Add(board[start.x, start.y]);
        id (d.contains(candidate)) print(candidate);
        if (d.containsprefix(candidate) && count < board.rows * board.cols)
               GetWordSnake(board, start, count+1, candidate, d);
        start = revert(start, i)
  }
}

Perform Topological Sort
Topological DFS(int[,] G, List<int> color, List<int> parent, ref List<int> result)
for (int i =0; i < G.rows; i++)
{
    color[i] = white;
    parent[i] = -1;
}
time = 0;
for (int j =0 ; j < G.rows; j++)
{
      if (color[j] == white)
         DFS-Visit(G, j, ref color, ref parent, ref time, def result)
}
}

DFS-Visit(int[,] G, int u, ref List<int> color, ref List<int> parent, ref int time,ref List<int> result)
{
time = time + 1;
//u.start = time;
color[i] = gray;
for (int i = 0; i < G.cols; i++)
   var v =  G[u, i];
       if v > 0 &&color[v] == white
           parent[v] = u;
           DFS-Visit(G, v, color, parent, time, result)
color[u] = black;
time = time + 1;
result.InsertAt(0, u);
//u.f = time
}


Friday, May 27, 2016

Storing backups of Virtual machines
This example is for a VMWare vCenter which allows virtual machines to be backed up and recovered.  Generally some space is reserved on the VMWare vCenter for storing backups of VMs provisioned on the vCenter. Typically about 20% of the storage on the vCenter is earmarked for this purpose.  A backup is typically a portable tar file comprising of vmdk and other relevant files.
The benefit of using the storage is that the vCenter keeps track of these files that can be queried and used from different clients through command line, API or web interface.  
However, clients can also choose to download these files to their repository of choice and manage it themselves. This is especially helpful if the archives have to be used outside of the vCenter they originated from.
An example of download is given from pyvmomi library
   def download_file(url, local_filename, cookie, headers):
        r = requests.get(url, stream=True, headers=headers,cookies=cookie,verify=False)
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024*1024*64):
                if chunk:
                    f.write(chunk)
                    f.flush()

        return local_filename

Now vmdks  can be exported with
    def export_vmdks(vm, prefix_clone, esxhost, server_name):
        import pyVmomi
        HttpNfcLease = vm.ExportVm()
        try:
            infos = HttpNfcLease.info
            device_urls = infos.deviceUrl
            vmdks = []        
            for device_url in device_urls:
                deviceId = device_url.key
                deviceUrlStr = device_url.url
                diskFileName =  vm.name.replace(prefix_clone,'') + "-" + device_url.targetId
                diskUrlStr = deviceUrlStr.replace("*", esxhost)
                diskLocalPath = './' + diskFileName

                cookie = make_compatible_cookie(si._stub.cookie)
                headers = {'Content-Type': 'application/octet-stream'}
                logger.debug("[%s] exporting disk: %s" %(server_name,diskFileName))
                
                download_file(diskUrlStr, diskFileName, cookie, headers)
                vmdks.append({"filename":diskFileName,"id":deviceId})
        finally:
            HttpNfcLease.Complete()
        return vmdks

#codingexercise
convert a binary tree into doubly linked list
Node BT2DLLHelper(Node root)
{
if (root==null) return root;
if (root.left != null)
{
Node left = BT2DLLHelper(root.left);
for (; left.right != null; left = left.right);
left.right = root;
root.left = left;
}
if (root.right != null)
{
Node right = BT2DLLHelper(root.right);
for (; right.left != null; right = right.left);
right.left = root;
root.right = right;
}
return root;
}
Node BT2DLL(Node root)
{
if (root == null) return root;
root = BT2DLLHelper (root);
for( ; root.left != null; root = root.left);
return root;
}