Interview Question
Country: India
You have a valid point. The test case of cloning a malformed list is required.
The correctness in expected output is ambiguous -
a) clone fails as soon as it detects a loop and returns NULL
b) clone completes by detecting the loop and returns a malformed list
c) clone completes by detecting and removing the loop in the cloned copy (source is read only).
What is required to be done is supplement the function to detect malformed list.
Lets assume each element in node is represented as -
Given a list you can clone it with the above structure -
Test cases would be -
- RS August 09, 20161. validListClone(list)
Expected output should produce a copy. Validate using the number of elements in source and destination should match
2. emptyListClone(NULL);
Expected output should produce a empty list
3. circularListClone(list);
Expected output should to produce a circular list with last node of the cloned list pointing to the first element of the cloned list and not the original list.