Showing posts with label Hashtable. Show all posts
Showing posts with label Hashtable. Show all posts

Friday, April 27, 2012

Java Interview Frequently Asked Questions & Answers (S1P1)



What is the difference between an Interface and an Abstract class?
          An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

What is the purpose of garbage collection in Java, and when is it used?
          The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Describe synchronization in respect to multithreading.
          With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.

Explain different way of using thread?
          The thread could be implemented by using runable interface or by inheriting from the Thread class. The former is more advantageous, because when you are going for multiple inheritance, the only interface can help.

What are pass by reference and pass by value?
          Pass By Reference means the passing the address itself rather than passing the value. Pass by Value means passing a copy of the value to be passed.

What is HashMap and Map?
          Map is Interface and Hashmap is class that implements that.

Difference between HashMap and HashTable?
          The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable does not allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.

Source: Here

Wednesday, August 17, 2011

Morgan Stanley Interview Question


Suppose you have a large file with lots of words. How would you find the unique words and their count?
What kind of data structure u will use? What will be the time complexity and space complexity?

/* Algorithm to count words in a file. Please dont reply opinions on the code, comments on complexity analysis is appreciated
For each word check if the word exists in the hash, if it doesnt then insert the word with a count 1
if it does then increment the value
For each word in the hashmap, print word and its count
Complexity: Linear insertion and space
NOTE:The code only compiles with newer C++ use the switch -std=c++0x in your Makefile*/
#include <iostream>
#include <vector>
#include <unordered_map>
typedef std::unordered_map<std::string, int> MyHashMap;
void printMyHashMap(MyHashMap::const_iterator itr,MyHashMap::const_iterator enditr){
while(itr!=enditr){
std::cout<<(*itr).first<<" : "<<(*itr).second<<std::endl;
++itr;
}
}
int main(){
MyHashMap wordsMap; //declare unordered map
//vector of strings
std::vector<std::string> strings {"count","words","in","a","file",
"and","print","words","with","their","count",
".","more","words","file"};
int len = strings.size();
std::vector<std::string>::const_iterator wordsItr;
wordsItr = strings.begin();
MyHashMap::iterator mapItr = wordsMap.begin();
for ( wordsItr=strings.begin(); wordsItr!=strings.end(); wordsItr++ ){
mapItr = wordsMap.find(*wordsItr);
if(mapItr==wordsMap.end()){ //if word not found then
wordsMap.insert(MyHashMap::value_type(*wordsItr, 1));
}else{
(*mapItr).second = ++(*mapItr).second; }
}
std::cout<<"words and their count :"<<std::endl;
printMyHashMap(wordsMap.begin(),wordsMap.end());
return (0);
}



///2
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <map>
#include <iterator>
using namespace std;
int main() {
string str;
ifstream in("input.txt");
map<string, int> m;
if (in.is_open()) {
while(in >> str) {
m[str]++;
}
}
for(map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << (*it).first <<"\t" << (*it).second << endl;
}
}
input.txt
abc abc abc abc abc abc abc abcdef abc abcd
abc abc abc abc abc abc abc abcdef abc abcd
abc abc abc abc abc abc abc abcdef abc abcd
abc abc abc abc abc abc abc abcdef abc abcd
count words in a file and print words with their count more words file
output
ajay@ubuntu:~/workspace$ ./a.out
a 1
abc 32
abcd 4
abcdef 4
and 1
count 2
file 2
in 1
more 1
print 1
their 1
with 1
words 3


//Java method


import java.util.HashMap;
public class TestTest {
public static void main(String[] args){
String inputString = "abc abc abc abc abc abc abc abcdef abc abcd abc abc abc abc abc abc abc abcdef abc abcd" +
" abc abc abc abc abc abc abc abcdef abc abcd abc abc abc abc abc abc abc abcdef abc abcd" +
" count words in a file and print words with their count more words file";
HashMap<Object, Integer> myMap = new HashMap<Object, Integer>(){
@Override
public Integer put(Object arg0, Integer arg1) {
Integer i = super.get(arg0);
arg1 = i==null?1:i+1;
return super.put(arg0, arg1);
}
};

String[] splits = inputString.split(" ");
for(String s : splits){
myMap.put(s,1);
}

System.out.println(myMap);
}
}


Source: Here


Thursday, May 26, 2011

Warm up question: What is the difference between Hashtable and Hashmap?

Notes on Hashtable and Hashmap.
      Both provide key-value access to data.
  • HashMap permits null value while Hashtable does not.
  • Access to Hashtable is synchronized on the table while access to the HashMap is not (by default).
  • Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not.
  • Hashtable is an original collection class in Java while HashMap is part of the new collections framework.