Data World

Archive for the ‘Interview Questions’ Category

Python : Playing with Linked list

Posted by Pramod Singla on May 14, 2018


  • Create linked list and add node at the beginning

class node:

    def __init__(self, p_data):

        self.data=p_data

        self.next=None

class linkedlist:

    def __init__(self):

        self.head=None

        self.len=0

    def addNodeAtBegin(self,data):

        newNode=node(data)

        newNode.next=self.head

        self.head=newNode

        self.len=self.len+1

    def print(self):

        node=self.head

        while node:

            print(node.data)

            node=node.next

newlinkedList=linkedlist()

newlinkedList.addNodeAtBegin(10)

newlinkedList.print()

 

  • Linked list and add node in sorted list

class node:

    def __init__(self, p_data):

        self.data=p_data

        self.next=None

class linkedlist:

    def __init__(self):

        self.head=None

        self.len=0

    def addNodeAtBegin(self,data):

        newNode=node(data)

        newNode.next=self.head

        self.head=newNode

        self.len=self.len+1

    def sortedInsert(self,data):

        newNode=node(data)

        Curr=self.head

        if Curr==None or Curr.data>newNode.data:

            newNode.next=self.head

            self.head=newNode

        else:

            Next=Curr.next

            while Curr.data<=newNode.data:

                if Curr.next==None:

                    Curr.next=newNode

                    newNode.next=None

                    break;

                if Curr.data<=newNode.data and Next.data>=newNode.data:

                    Curr.next=newNode

                    newNode.next=Next

                    self.len=self.len+1

                    break

                Curr=Next

                Next=Next.next

    def print(self):

        node=self.head

        while node:

            print(node.data)

            node=node.next

newSortedlinkedList=linkedlist()

newSortedlinkedList.sortedInsert(10)

newSortedlinkedList.sortedInsert(20)

newSortedlinkedList.sortedInsert(30)

newSortedlinkedList.sortedInsert(15)

newSortedlinkedList.sortedInsert(10)

newSortedlinkedList.sortedInsert(30)

newSortedlinkedList.sortedInsert(40)

newSortedlinkedList.sortedInsert(5)

newSortedlinkedList.print()

 

Advertisements

Posted in Interview Questions, Python | Tagged: , | Leave a Comment »

Python: Find Whether A String Could be Palindrome or Not

Posted by Pramod Singla on May 14, 2018


str=”amalaylam”

flag=0

for i, c in enumerate(str):

    print (i,c,ascii(c),str.count(c))

    if (str.count(c))%2 != 0:

        flag=flag+1;

        if flag>1:

            print(” Not palindrome”)

            break;

        continue;

if flag<=1:

    print(”  palindrome”)

Posted in Interview Questions, Python | Tagged: , , | Leave a Comment »

Data Structure and Algorithm Interview Questions With Python

Posted by Pramod Singla on May 14, 2018


  1. Find whether a string could be palindrome or not?
  2. Linked list creation with add node at the beginning
  3. Linked list creation and add node in sorted list

Posted in Interview Questions, Python | Leave a Comment »

SQL Server Tuning – Top 10 high memory consuming Queries

Posted by Pramod Singla on March 23, 2017


Problem Statement: Script to find top 10 high memory consuming Queries.

Solution:

SELECT TOP 10 Object_name(qt.objectid) 
              AS 'SP Name', 
              Substring(qt.text, ( qs.statement_start_offset / 2 ) + 1, 
              ( 
              ( CASE qs.statement_end_offset 
              WHEN -1 THEN Datalength(qt.text) 
              ELSE qs.statement_end_offset 
                                                                            END 
                - 
                qs.statement_start_offset ) / 2 ) + 1) 
              AS statement_text, 
              total_logical_reads, 
              qs.execution_count 
              AS 'Execution Count', 
              total_logical_reads / qs.execution_count 
              AS 'AvgLogicalReads', 
              qs.execution_count / Datediff(minute, qs.creation_time, Getdate()) 
              AS 
              'Calls/minute', 
              qs.total_worker_time / qs.execution_count 
              AS 'AvgWorkerTime', 
              qs.total_worker_time 
              AS 'TotalWorkerTime', 
              qs.total_elapsed_time / qs.execution_count 
              AS 'AvgElapsedTime', 
              qs.total_logical_writes, 
              qs.max_logical_reads, 
              qs.max_logical_writes, 
              qs.total_physical_reads, 
              qt.dbid, 
              qp.query_plan 
FROM   sys.dm_exec_query_stats AS qs 
       CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS qt 
       OUTER APPLY sys.dm_exec_query_plan(qs.plan_handle) qp 
WHERE  qt.dbid = Db_id() -- Filter by current database 
ORDER  BY total_logical_reads DESC  

Posted in DBA, Interview Questions, Performance Tuning, Scripts | Tagged: , , , , | Leave a Comment »

SQL Server -Find Nth Highest Salary

Posted by Pramod Singla on August 15, 2014


Problem: Find Nth highest salary in Minimal time.

Solution: Interviewer often asks to find Nth Highest Salary in an interview. Many solutions are available on internet to do so. Couple of commonly used solutions are Solution 1 & Solution 2.

All these solutions are bit slow. But by using Common Expression Tables (CTE) you can find the Nth highest salary very efficiently and impress your interviewer.

  • Create a Sample Table and  Insert Sample Data in it:

CREATE TABLE #T1 (ID1 INT, [Name] NVARCHAR(50), Salary INT)

After creating the table, execute the above code to insert the data into the table as follows:

INSERT INTO #T1 VALUES (1, ‘pds’, 10000)
INSERT INTO #T1 VALUES (2, ‘vicky’, 10000)
INSERT INTO #T1 VALUES (3, ‘divyan’, 30000)
INSERT INTO #T1 VALUES (4, ‘deepak’, 40000)
INSERT INTO #T1 VALUES (5, ‘anu’, 50000)
INSERT INTO #T1 VALUES (6, ‘jai’, 60000)
INSERT INTO #T1 VALUES (7, ‘abhi’, 20000)
INSERT INTO #T1 VALUES (8, ‘disha’, 40000)
INSERT INTO #T1 VALUES (9, ‘anika’, 60000)
INSERT INTO #T1 VALUES (10, ‘vijay’, 50000)

To find the highest salary we can simply use the Max function as given below:

Select Max(Salary) as [Highest Salary] from T1

  • Following Query will Find Nth Highest Salary in minimal time:

         declare @n int;

        set @n =4

        with cte as

       (

          SELECT name, salary,
                     dense_rank() OVER( ORDER BY salary DESC) AS rowid
                   FROM   #t1
              )

     SELECT *
    FROM   cte
    WHERE  rowid = @n

     This will create row number for each row after it has been sorted by salary in the descending order.

  • Comparison Stats with Other Solutions:

Posted in Interview Questions | Tagged: , , , | 4 Comments »