ADP Interview Question
Graphics ProgrammersCountry: United States
Interview Type: In-Person
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;
}
}
}
}
}
- Sagnik Majumder February 14, 2012