veerun14
BAN USERpublic static char secondMaxCount(char[] str)
{
int[] intArray = new int[256];
int count = 1;
int MaxCount = 0;
int SecondMaxCount = 0;
int MaxCharIndex = -1;
int SecondMaxCharIndex = -1;
int i = 0;
while (i < str.Length)
{
if (intArray[str[i]] == 0)
intArray[str[i++]] = count;
else
intArray[str[i++]]++;
}
i--;
while (i >= 0)
{
if (intArray[str[i]] > MaxCount)
{
SecondMaxCount = MaxCount;
MaxCount = intArray[str[i]];
SecondMaxCharIndex = MaxCharIndex;
MaxCharIndex = i;
i--; ;
}
else if (intArray[str[i]] > SecondMaxCount && intArray[str[i]] != MaxCount)
{
SecondMaxCount = intArray[str[i]];
SecondMaxCharIndex = i;
i--;
}
else
i--;
}
if (SecondMaxCharIndex < 0)
return '?';
else
return str[SecondMaxCharIndex];
}
let me know if there are any bugs....
How about using a graph? All the cities(nodes) are connected with edges with the cost of the edge representing the price of the delivery.
With this data structure you can even get the price if the delivery is A to B via C. The graph can be implemented through adjaceny list. Although we face with repititions the look up of the price is super fast...
well it has different meanings in different contexts..
when used with a member in a user-defined data structure such as a class, it specifies that the member is of that type itself rather than for every specific object of that class
when uses with a variable in a function, it retains its state between the function calls
for example:
void test(int current)
{
static int i; //initially will be zero if nothing is initailised
current = current + i;
print current;
}
int main()
{
for(int i=0;i<5;i++)
test(i);
}
the output should be 0 1 3 6 10
questions questions
1.which country is this parking lot in
2.what is the area i am playing with
3.what could be the maximum number of slots to be provided
4.is it an underground parking lot or around a building
5.is the parking lot metered or non-metered
6.where exactly is the location of the lot w.r.t the road
7.IF it is at a corner (where two roads meet) how many exit and entry points you need
8.What kind(size) of vehicles are going to occupy the lot
9.if it is metered can the users pay with credit cards and cash too
my idea is to use a priority queue, ie a heap, in fact a max heap. a max heap is a tree like structure where the root of the tree (heap) is the maximum element.
so for example a heap (nothing but a complete binary tree except lower level) is like this
55
50 45
18 35 40 39
17
50 and 45 children of 55
18 35 children of 50
40 39 children of 45
17 child of 18
if you see the tree, it is completely filled except the bottom level (which is nothing but a heap, in this case a max heap with the maximum element at root)
Now simply do an array implementation
the array would be 55 50 45 18 35 40 39 17
max element---is arr[0]
dequeue---remove first element and check for the next biggest element. This is not that difficult...remember percolate down approach
enqueue is always done at the end of the array but again maintaining heap structure and order property
I think what the interviewer wants boggles down to implementation of a max priority queue i.e a max heap
int* reverse(int* arr, int n)
{
if(n<0)
return arr;
else
{
int s = 0;
int e = n-1;
for(s=0;e>s;s++,e--)
{
arr[s] ^= arr[e];
arr[e] ^= arr[s];
arr[s] ^= arr[e];
}
return arr;
}
}
basically doing bitwise XOR operation, there by needing no temp to swap the numbers. bit faster than recursive though
algo
get the least significant digit by using successive modulo and / operator and keep pushing at the head end of the list.
Test cases
1. what to do when the number is negative...where to store the - sign
2. what if the value is 0. no need to perform the % and / operator. create a single node with data value equal to 0
3.what if the number is out of range
4. what if the number is float (given it is an unsigned int, but does your logic handle for a wrong input??)
5.check for memory allocation of the node, can be a failure sometimes
6.similarly check for a wrong input of characters instead of digits
you can use a hash table with key equal to each character and value getting incremented every time you have a key collision. That way for small strings your program would use a neat and small hashtable.
even for a million characters (when the string is a huge paragraph) the maximum the hash table can get to is 65,535 for unicode characters and a simple 255 for ascii characters.
So, even for large and small strings, the approach with hash table is easier
Test cases
1. First and foremost, when you are dealing with characters, don't forget the case sensitive issues
2. Then check for empty string
3. What to do when string is abc---result should be a1b1c1
4. what do if the string is abababcdcdcd---should it be ab3cd3 or a3b3c3d3,,,ask for it the repeated characters are side by side
5. how to deal with white space characters
6. check if the code is efficient for 1 million characters
7. what happens if the code is run for 1 month continuously
8. what about specail characters and roman numericals and etc,...
9. check for entry and exit locations of the routine
10. check for interface testing..are any unusual things happening when interfaced with a larger routine
like muhammd said
start by testing all possible scenarios for each of the parts in the system
queues, processor, and logger
for queues check enqueue dequeue operations,case of empty queues,
for processor, cases like read failure write failure, what happens if the power is lost during reading, during writing, what back up mechanism?? make sure the processor reads and writes at exactly hour apart.
a logger can have several scenarios since it connects to the database. what happens when there are no files, what does it write to database, power failures, back up mechanisms, database failures, connection failures,
Then go to the interface testing. what will the processor do if the queue is empty.
if it fails to read data, then what happens to the logger, what would it write to the database.
Go through all the lines one by one and put each line in a node of a single linked list.
- veerun14 September 14, 2015Then, take two pointers, one starting at head, and other starting at a distance of n from head
At the end of one traversal of the linked list, you have the first pointer at a distance of n from the end and second pointer at the end.
Just print the lines