Showing posts with label .Net Interview. Show all posts
Showing posts with label .Net Interview. Show all posts

Saturday, February 22, 2020

how to build a link list tree in C#?

i had completed a hackerrank challenge a week ago.. it is quite interesting topic. since C# already provide the LinkList object in the .net framework.

i need to use c# to build my own LinkList class. the recursion is the key to solve this challenge.

here is the sample code. Happy programming


using System;
class Node
{
public int data;
public Node next;
    public Node(int d){
        data=d;
        next=null;
    }
}
class Solution {
public static  Node insert(Node head,int data)
{
      //Complete this method
      Node newNode=new Node(data);
      if(head==null){
         // Console.WriteLine($"{newNode.data} ,{data}");
          return newNode;
        }
      else if(head.next ==null){
          //Console.WriteLine($"{newNode.data} ,{data}");
            head.next=newNode;
       }
       else insert(head.next, data);
      return head;
      //Console.WriteLine($"{head.data} ,{data}");
      //return head;
    }
public static void display(Node head)
{
Node start=head;
while(start!=null)
{
Console.Write(start.data+" ");
start=start.next;
}
}
    static void Main(String[] args) {
Node head=null;
        int T=Int32.Parse(Console.ReadLine());
        while(T-->0){
            int data=Int32.Parse(Console.ReadLine());
            head=insert(head,data);
        }
display(head);
}
}


Friday, February 21, 2020

how to use Queue to implement a binary searh true Level Order algorithm?

Currently i had joined the Hackerrank challenag for 30 days

today i got a very interesting topic to use Queue to implement the level Order function

using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; class Node{ public Node left,right; public int data; public Node(int data){ this.data=data; left=right=null; } } class Solution{ static void levelOrder(Node root){ Queue<Node> nodeQueue=new Queue<Node>(); if(root !=null){ nodeQueue.Enqueue(root); } while(nodeQueue.Count>0){ Node current=nodeQueue.Dequeue(); Console.Write($"{current.data} "); if(current.left !=null){ nodeQueue.Enqueue(current.left); } if(current.right !=null){ nodeQueue.Enqueue(current.right); } } } static Node insert(Node root, int data){ if(root==null){ return new Node(data); } else{ Node cur; if(data<=root.data){ cur=insert(root.left,data); root.left=cur; } else{ cur=insert(root.right,data); root.right=cur; } return root; } } static void Main(String[] args){ Node root=null; int T=Int32.Parse(Console.ReadLine()); while(T-->0){ int data=Int32.Parse(Console.ReadLine()); root=insert(root,data); } levelOrder(root); } }

Wednesday, January 22, 2020

Tricky C# interview question

I had been asked to trace the following code for the output

  class Program
    {
        public delegate void Iterator();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            List<Iterator> interators = new List<Iterator>();
            for (int i = 0; i < 15; i++)
            {
                interators.Add(delegate { Console.WriteLine(i); });
            }

            foreach (var interator in interators)
            {
                interator();
            }
            Console.Read();
        }
    }

Do you like to try it?