ADP Interview Question for Graphics Programmers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

enum DocumentType
{
     PdfDoc,
     WordDoc
};
class Document
{
      public:
             virtual DocumentType Type() = 0;
};
class PdfDocument : public Document
{
      public:
             DocumentType Type()
             {
                  return PdfDoc;
             }
};
class WordDocument : public Document
{
      public:
             DocumentType Type()
             {
                  return WordDoc;
             }      
};
struct Node
{
       Document *documentp;
       Node *nextp;
   //    Node(Document *d, Node *n = 0): documentp(d),nextp(n)
   //    { }
};

class PrintQueue {
      public:
             PrintQueue();
             ~PrintQueue();
 
             void Push(Document *docp);
             Document* Pop();
             bool IsEmpty() const;
      private:
             Node *_frontp, *_rearp;
};

PrintQueue::PrintQueue(): _frontp(0), _rearp(0)
{
     cout<<"PrintQueue Constructor!"<<endl;
}
PrintQueue::~PrintQueue() {
       Node *curp, *oldp;
       if (IsEmpty())
          return;
       curp = _frontp;
       while (curp)
       {
              oldp = curp;
              curp = curp->nextp;
              delete oldp;
       }
}

bool PrintQueue::IsEmpty() const {
 return (_frontp == 0);
}

void PrintQueue::Push(Document *docp) {
  assert(docp != 0);
//  Node * nodep = new Node(docp);
  Node * nodep = new Node();
  nodep->documentp = docp;
  nodep->nextp = 0;
  if (IsEmpty())
    _frontp = _rearp = nodep;
  else
  {
    _rearp->nextp = nodep;
    _rearp = nodep;
  }
}

Document * PrintQueue::Pop() {
// if (IsEmpty())
//    throw PQUnderflowException();
 
 Node * nodep = _frontp;
 _frontp = _frontp->nextp;
 if (IsEmpty())
  _frontp = _rearp = 0; 
 Document * docp = nodep->documentp;
 delete nodep;
 return docp;
}


/*
 * main
 */
int main(int argc, char *argv[]) {
 PrintQueue obj;
 
  Document * word1 = new WordDocument();
  Document * pdf1 = new PdfDocument();
  Document * word2 = new WordDocument();
  Document * pdf2 = new PdfDocument();
  Document * word3 = new WordDocument();
  Document * pdf3 = new PdfDocument();
  Document * word4 = new WordDocument();
  Document * pdf4 = new PdfDocument();
  
  cout << "document type = " << word1->Type()<< endl;
  cout << "document type = " << pdf1->Type()<< endl;
  cout << "document type = " << word2->Type()<< endl;
  cout << "document type = " << pdf2->Type()<< endl;
  cout << "document type = " << word3->Type()<< endl;
  cout << "document type = " << pdf3->Type()<< endl;
  cout << "document type = " << word4->Type()<< endl;
  cout << "document type = " << pdf4->Type()<< endl;      
    
  obj.Push(word1);
  obj.Push(pdf1);
  obj.Push(word3);
  obj.Push(word2);
  obj.Push(word2);
  obj.Push(pdf3);    
  
  Document * docp1 = obj.Pop();
  cout << "popped document type = " << (docp1->Type() ? "Word" : "Pdf") << endl;
  Document * docp2 = obj.Pop();
  cout << "popped document type = " << (docp2->Type() ? "Word" : "Pdf") << endl;
  Document * docp3 = obj.Pop();
  cout << "popped document type = " << (docp3->Type() ? "Word" : "Pdf") << endl;    
  Document * docp4 = obj.Pop();
  cout << "popped document type = " << (docp4->Type() ? "Word" : "Pdf") << endl;
  Document * docp5 = obj.Pop();
  cout << "popped document type = " << (docp5->Type() ? "Word" : "Pdf") << endl;
  Document * docp6 = obj.Pop();
  cout << "popped document type = " << (docp6->Type() ? "Word" : "Pdf") << endl;  

 delete word1,word2,word3,word4, pdf1, pdf2, pdf3, pdf4;
 getchar(); 
 return 0;
}

- Sagnik Majumder February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does anybody solved this problem in C# ?
just need help in C# ...

- Deb October 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestDocumentQueue
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintQueue pq = null;

            while (true)
            {
                Console.WriteLine("Enter Your Choice:");
                Console.WriteLine("1.Push.");
                Console.WriteLine("2.Pop.");
                Console.WriteLine("3.Process Document.");
                Console.WriteLine("4.Exit.");

                int choice = int.Parse(Console.ReadLine());

                if (pq == null)
                    pq = new PrintQueue();

                switch (choice)
                {
                    case 1:
                        Console.WriteLine("Enter subject: ");
                        string s = Console.ReadLine();
                        Console.WriteLine("Enter type: ");
                        string t = Console.ReadLine();
                        switch (t)
                        {
                            case "Pdf":
                                PdfDocument pd = new PdfDocument(s);
                                pq.Push(pd);
                                break;
                            case "Word":
                                WordDocument wd = new WordDocument(s);
                                pq.Push(wd);
                                break;
                            default:
                                Console.WriteLine("Invalid Choice.");
                                break;
                        }
                        break;
                    case 2:
                        Document d = pq.Pop();
                        if (d == null)
                            Console.WriteLine("Empty.");
                        else
                            Console.WriteLine("Subject: {0}, Type: {1}", d.Name(), d.Type());
                        break;
                    case 3:
                        pq.ProcessDocument();
                        break;
                    case 4:
                        Environment.Exit(0);
                        break;
                    default:
                        Console.WriteLine("Invalid Choice.");
                        break;
                }
            }
        }
    }
    class Document
    {
        private string subject;
        private string type;

        public Document(string n, string t)
        {
            subject = n; type = t;
        }

        public virtual string Name()
        {
            return subject;
        }
        public virtual string Type()
        {
            return type;
        }
    }
    class PdfDocument : Document
    {
        public PdfDocument(string n)
            : base(n, "Pdf")
        {
        }
        public override string Name()
        {
            return base.Name();
        }
        public override string Type()
        {
            return base.Type();
        }
    }
    class WordDocument : Document
    {
        public WordDocument(string n)
            : base(n, "Word")
        {
        }
        public override string Name()
        {
            return base.Name();
        }
        public override string Type()
        {
            return base.Type();
        }
    }
    class DocumentNode
    {
        public Document Info
        {
            get;
            set;
        }
        public DocumentNode Next
        {
            get;
            set;
        }
    }
    class PrintQueue
    {
        private DocumentNode head;
        public PrintQueue()
        {
            head = new DocumentNode();
            head.Next = null;
        }
        public void Push(Document d)
        {
            DocumentNode current;
            current = head;
            while (current.Next != null)
            {
                current = current.Next;
            }
            DocumentNode newNode = new DocumentNode();
            newNode.Info = d;
            newNode.Next = null;
            current.Next = newNode;
            if (current == head)
            {
                head = current;
            }
        }
        public Document Pop()
        {
            DocumentNode current;
            DocumentNode prev = null;
            Document d=null;
            current = head;
            while (current.Next != null)
            {
                prev = current;
                current = current.Next;
            }
            if (current != head)
            {
                prev.Next = null;
                d = current.Info;
            }
            return d;
        }
        public void ProcessDocument()
        {
            if (head.Next != null)
            {
                DocumentNode current = head.Next;
                int id = 1;
                while (current != null)
                {
                    Console.Write("Subject Line of Document {0} is {1}, Type is {2}", id, current.Info.Name(), current.Info.Type() + '\n');
                    id++;
                    current = current.Next;
                }
            }
        }
    }
}

- Soumava Bose November 16, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More