Develop an algorithm which would determine whether the given input string is a palindrome or not. However there are certain constraints on the algorithm. The constraints are as follows -
- We do not know the length of the input string.
- We can read each character of the input string only once (I think the implication of this constraint along with the previous one should be clear).
- We have to employ constant space.
This problem is very interesting because its solution employs concepts which every computer engineer must have studied but very few will be able to connect the problem with it.
If we look carefully at the above constraints, we will recognize that they together specify a Deterministic Finite Automata. In other words, we are asked to recognize whether a string is palindrome or not through a DFA. This is clearly not possible.
Lets see how each constraint maps the required automaton to a DFA. (1) is just given to disallow quirks. (2) ensures that you cannot move back and forth on the input tape. This is again a characteristic of DFA where you scan symbols on input tape only once. (3) is again a characteristic of DFA, we should have finite states.
Hope you enjoyed the problem.
Suppose we are given an array having 2n +1 elements such that among these 2n + 1 elements, we have n pairs of x and -x. Only one number y is there whose minus is not there. Find the number y.
Following is a python solution for the problem.Just do the xor of all the elements in the array. Whatever remains is the number which has no duplicate.
1 def find_non_duplicate(array): 2 result = array[0] 3 for i in array[1:]: 4 result ^= i 5 return result
1 int find_non_duplicate(int *array, int size) 2 { 3 int result = array[0]; 4 int i; 5 for (i = 1; i < size; i++) 6 result ^= array[i]; 7 return result; 8 }
There are three prime numbers, p, q and r. Further p and q are consecutive prime numbers. In other words, there is no prime number k such that p < k < q. We are also given that p + q = 2r. Find the smallest such triplet (p, q, r).
We are given a singly-linked list A. The nodes of A consists of usual data and next pointer. Along with these there is another pointer random which points to a random node of A. We are supposed to make a new linked list B which is copy of A as far as the structure is concerned but all its nodes are newly allocated. Develop an algorithm such that its time complexity is O(n).
t->data = i_s->data;
t->next = i_s->random;
t->random = NULL;
i_s->random = t;
i_s = i_s->next;
}
/* SECOND PASS: Adjust all new nodes to have there
* correct random pointers
*/
i_s = src->next;
while(i_s)
{
Node *n;
n = i_s->random;
n->random = (n->next)->random;
i_s = i_s->next;
}
/* THIRD PASS: Unzip the teo link lists */
i_s = src->next;
i_d = *dst;
while(i_s)
{
t = i_s->random;
i_s->random = t->next;
t->next = NULL;
i_d->next = t;
i_d = t;
i_s = i_s->next;
}
}
/* * multi_space_compressor * * compresses multiple white space characters to single * white space * */
#include <assert.h> #include <ctype.h> #include <malloc.h> const unsigned int MOFIFY_ORIGINAL = 1<<0; char *multi_space_compressor(char *src, const unsigned int params) { int last_char_space = 0; char cur_char; char *dst; int i = 0; if(!(params && MOFIFY_ORIGINAL)) { /* do a pass to determine the length and allocate the string */ int i, req_chars = 0; for(i=0; (cur_char = *(src + i)); i++) { if(!isspace(cur_char)) { /* non white space */ req_chars ++; last_char_space = 0; } else if (!last_char_space) { req_chars ++; last_char_space = 1; } } dst = (char *)malloc(req_chars + 1); assert(dst != 0); } else dst = src; last_char_space = 0; i = 0; for(;(cur_char = *src);src++) { if(!isspace(cur_char)) { /* non white space */ *(dst + i) = cur_char; last_char_space = 0; i++; } else if(!last_char_space) { *(dst + i) = ' '; last_char_space = 1; i++; } } *(dst + i) = '\0'; return dst; }