Employee Register

Design a custom data structure that functions as a doubly linked list (DLL) to manage employee records. Each node in the list represents an employee and contains their SSN (unique string), name, department, designation, salary (integer), and phone_number.

The system provides a hidden Node struct (which you cannot edit) containing these data fields alongside prev and next pointers. The exact structure definition is provided as a comment in your code editor.

You must implement the EmployeeDLL class with the following methods:

  • EmployeeDLL(): Initializes an empty doubly linked list.
  • void addFront(string ssn, string name, string dept, string desig, int sal, string phone): Creates a new employee record and inserts it at the front of the list. This operation is silent (returns nothing).
  • void addBack(string ssn, string name, string dept, string desig, int sal, string phone): Creates a new employee record and appends it to the back of the list. This operation is silent.
  • string removeFront(): Removes the employee record at the front of the list. Returns the SSN of the removed employee. If the list is empty, return "UNDERFLOW".
  • string removeBack(): Removes the employee record at the back of the list. Returns the SSN of the removed employee. If the list is empty, return "UNDERFLOW".
  • int count(): Returns the total number of employee records currently in the list.
  • vector<string> printForward(): Returns an array of strings representing all records from front to back. Format each string strictly as "SSN,name,department,designation,salary,phone" (comma-separated, no spaces). If the list is empty, return an array containing a single string "EMPTY".
  • vector<string> printReverse(): Returns an array of strings representing all records from back to front, using the exact same formatting as above. If empty, return ["EMPTY"].

Note: All string inputs will consist of alphanumeric characters without spaces (e.g., "Software_Engineer").

Example 1:

Input:

[["EmployeeDLL"], ["count"], ["addFront", "111", "Alice", "IT", "SDE1", "90000", "555-0001"], ["addBack", "222", "Bob", "HR", "Manager", "80000", "555-0002"], ["count"], ["printForward"], ["removeFront"], ["removeBack"], ["removeBack"]]

Output:

[["null"], ["0"], ["null"], ["null"], ["2"], ["111,Alice,IT,SDE1,90000,555-0001", "222,Bob,HR,Manager,80000,555-0002"], ["111"], ["222"], ["UNDERFLOW"]]

Explanation:

  • EmployeeDLL(): Initializes the list. -> null
  • count(): List is empty. -> 0
  • addFront("111", ...): Adds Alice to the front. -> null
  • addBack("222", ...): Adds Bob to the back. -> null
  • count(): There are 2 records. -> 2
  • printForward(): Returns the list of 2 records in order. -> ["111,...", "222,..."]
  • removeFront(): Removes Alice and returns her SSN. -> "111"
  • removeBack(): Removes Bob and returns his SSN. -> "222"
  • removeBack(): List is empty. -> "UNDERFLOW"

Example 2:

Input:

[["EmployeeDLL"], ["removeFront"], ["addBack", "999", "Eve", "Sales", "Rep", "50000", "555-1234"], ["printReverse"], ["addFront", "888", "Dan", "Sales", "Lead", "75000", "555-9999"], ["printReverse"]]

Output:

[["null"], ["UNDERFLOW"], ["null"], ["999,Eve,Sales,Rep,50000,555-1234"], ["null"], ["999,Eve,Sales,Rep,50000,555-1234", "888,Dan,Sales,Lead,75000,555-9999"]]

Explanation:

  • EmployeeDLL(): Initializes the list. -> null
  • removeFront(): The list is currently empty. -> "UNDERFLOW"
  • addBack("999", ...): Adds Eve to the list. -> null
  • printReverse(): Only one record exists. -> ["999,Eve,Sales,Rep,50000,555-1234"]
  • addFront("888", ...): Adds Dan to the front. The list order from front to back is now [Dan, Eve]. -> null
  • printReverse(): Traverses from back to front. Eve is at the back (tail), Dan is at the front (head). -> ["999,Eve,Sales,Rep,50000,555-1234", "888,Dan,Sales,Lead,75000,555-9999"]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= count() <= 105
  • 0 <= salary <= 107
  • String lengths for SSN, name, department, designation, and phone will be between 1 and 50.
  • At most 105 total calls will be made across all methods.
  • addFront, addBack, removeFront, and removeBack must run in O(1) time complexity. count must run in O(1) time. printForward and printReverse must run in O(N) time.
0
/*
// Hidden Node struct provided by the system:
struct Node {
string SSN, name, department, designation, phone;
int salary;
Node* prev;
Node* next;
// Constructor initializes all fields and sets prev = next = nullptr
};
*/
 
class EmployeeDLL {
// TIP: Declare your head, tail, and size variables here!
public:
EmployeeDLL() {
// Your code goes here
}
void addFront(string ssn, string name, string dept, string desig, int sal, string phone) {
// Your code goes here
}
void addBack(string ssn, string name, string dept, string desig, int sal, string phone) {
// Your code goes here
}
string removeFront() {
// Your code goes here
}
 
string removeBack() {
// Your code goes here
}
int count() {
// Your code goes here
}
 
vector<string> printForward() {
// Your code goes here
}
 
vector<string> printReverse() {
// Your code goes here
}
};
Test Case

Input:

Input