You are on page 1of 4

Count characters in a string, wanted to see a hashtable used.

RE: Count characters in a str...


Monday, January 23, 2006 6:07 PM | Jack
Use a load factor of 1.5. The hashtable will stores integers(initially 0
). Since each ascii character has a unique ascii code, the key value will be ('c
haracter'-'a')%size. Increment the mapped-entry value if a hit. If character not
in table, resize.
RE: Count characters in a str...
Monday, January 23, 2006 6:10 PM | Jack
...the reason I did 1.5 is...
If the initial size of the table is 2. Then it expands to 3. If the initial size
is 1, then it expands to 2(round up). After 3, it resizes by more than 1.
RE: Count characters in a str...
Friday, February 17, 2006 12:27 PM | Bashar
public static Hashtable count(String s){
Hashtable table = new Hashtable();
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
Object o = table.put(new Character(c), new Integer(1));
if (o != null)
{int count = ((Integer)o).intValue() + 1;
table.put(new Character(c), new Integer(count));
}
}
return table;
}
RE: Count characters in a str...
Friday, February 17, 2006 12:49 PM | Jack
Actually, since the ascii character set has 2^8=256 possibilities, creat
e an array of 256 unsigned ints. Then index by character.
RE: Count characters in a str...
Wednesday, June 14, 2006 9:50 AM | Answer Dude
Wrong jack, you only need a table of 128 unsigned ints =). The last bit
in ascii is for error correction.

count bits in an integer. Solved using mask, did not attempt -1 approach.
E: count bits in an integer....
Monday, January 23, 2006 6:31 PM | Jack
Given x.
Although this is probably inefficient by the interviewer's stds, you could do th
is...
short numbits=0;
if(x<0)
numbits=1;

x <<= 1;
while(x!=0)
{
if(x<0)
numbits++;
x<<=1;
}
RE: count bits in an integer....
Monday, January 23, 2006 6:58 PM | Jack
One way to incorporate masking would be to group the bits into hex digit
s. Then, a hex digit has 0<= bits set<= 4.
RE: count bits in an integer....
Sunday, February 19, 2006 7:05 PM | Ajay
int MathC::countBits(unsigned int num){
int count=0;
while(num!=0){
count+=num & 1;
num=num>>1;
}
return count;
}
RE: count bits in an integer....
Wednesday, June 14, 2006 9:25 AM | Answer Dude
You can shorten the loop by using a lookup table and checking say 8 bits
at a time. I seem to remember something from discrete math that wouldn't requir
e a loop.. not sure though. Anyone know?

Reverse a linked list


Actually there is a very well written version from book "fundamentals of data st
ructures in C".
list_pointer invert (list_pointer lead)
{
/* invert the list pointed to by lead */
list_pointer middle, trail;
middle = null;
while (lead) {
tail = middle;
middle = lead;
lead = lead -> link;
middle->link = trail;
}
return middle;
}

Given an array of integers where every int has exactly one duplicate except one,
find the number with odd occuring number.
Shouldn't { 2*n*(n+1)/2 - Array Sum } work
This was a hard problem until Khoa mentioned thinking about the numbers
on a binary level. Since each number XOR'ed by itself will clear it's set bits,
you can go through the array XORing each number and end up with the number witho
ut a pair.
Consider the example:
2,3,2,4,3
0010
0011
---0001
0010
---0011
0100
---0111
0011
---0100

//
//
//
//
//
//
//
//
//
//
//
//
//

2
3
XOR'ed
result
2
XOR'ed
result: 3 (the odd number so far)
4
XOR'ed
result
3
XOR'ed
result 4 (The odd number in the sequence)

find dup number for n+1 numbers from 1...n


Number = Sum - n*(n+1)/2
Write code to shuffle a list of songs randomly and return the shuffled list
execute random function modulo number of songs
the return value will be the song to be played.
in case of getting already scheduled sone, re-execute the function or add 1 in a
cyclic way(maximum, number of songs) until you reach not sceduled song.
add the song element to a list when picked

Save the already picked somg in an array of BOOL that its indexes are the song n
umber. TRUE for picked song. that array will allow you to check whether the rand
om result song already picked or not
Copy the 4 to 3, and delete that node using the pointer pointing to 4 now (point
ing to 3 before)..
RE: You have a singly linked ...
Thursday, March 23, 2006 11:10 PM | yoyo
node* temp = list->next;
if(temp){
list->value = temp->value;
list->next = temp->next;
delete temp;
}
You have a singly linked list say 1->2->3->4->5 and you have no access to its he
ad pointer. But you have access to another pointer which points to the node 3. H
ow would you delete node 3 and get the output at 1->2->4->5. Remember its a sing

ly linked list

You might also like