1) Given a Char of “This is my name”, come up with a Char of “name my is this”. The program should use as less memory as possible
Soln: First reverse the whole Char using Char temp, the new Char will be “eman ym si siht” and then reverse each word so the resultant Char will be “name my is this”
2) From the website read data and then post it to a URL, you don’t have any control over the data that you are reading from the website, means the data size can be anything and it can take min, hour to complete.
So write a program so that you timeout on a pre determined time while posting the data to the URL.
HttpURLConnection setConnectionTimeOut, setReadTimeout are good for what they have meant for, but what if the posting of data it self takes a lot of time and you want to set the time out on that particular operation.
Soln: Create a new Thread in your main thread, log the start time and then start a new task in the new Thread which reads the data posted on the website and then open a connection to the url and then post the data to that url.
Outside of the Thread that is in the main thread check the time see if the timeOut time is equal to CurrentTime – startTime if not then in a while loop Sleep for some constant interval wake up and see the currentTime – StartTime is equal to timeOut or if the thread is no longer alive.
If the timeout time has been passed and thread is still alive then stop the thread, or if thread is completed before the timeout time has been passed, then exit gracefully.
3) Create a webpage which will pull all the files from a given directory in the filesystem.
Once user selects a file display the first 2 line and last 2 line of the file.
4) There are 5 millions records in a CSV file.
1) userId1, movies, sports
2) userId2, news, movies
a) So basically each record has a userId and then set of tags associated with them.
Come up with number of user associated to each tag.
1) Start reading the file read something like 200 record give to ExecutorService as a task to parse them and then store the results in a hashmap (key – tag, value – counter)
2) Consolidate all the hashmap value into one.
Use something like synchronizers like CountDownLatch instead of wait and notify for figuring out when all the task has been complete and then consolidate.
I gave the answer that read one line at a time and send it to Executor service as a task and add the counter to the concurrent hash map, but here the problem is because we are using concurrentHashMap we will be blocked to increment the counter.
b) Now tag are associated with one or more tag like TV is associated with soap and sports and then sports is associated with cricket and tennis and so on.
So now find all the people associated with a tag and their associated tag, so basically find all the people associated with TV, soap, sports, cricket, tennis
Create a list of tag that is associated with a given tag
Start parsing the file and then userId in a list if any one of his tag is the tag list
5) You have a list of number 1 to 9
1 2 3 4 5 6 7 8 9
Now all you can do is either you can combine the number like 12 or 23 or 34 or you can add a + symbol between 1 and 2, 3 and 4
So come up with the combination and + sign so that
1 2 3 4 5 6 7 8 9 = 99
1 + 2 + 3 + 4 + 5 + 67 + 8 + 9 = 99
6) You have a stack of number, at any given point of time tell what is the minimum number available in the stack
So for Example the stack is like this
So in this case the minimum number is 4, once you pop out 5 the minimum is still 4 and then when you pop out 4 the minimum is 7
maintain two stack so this is how they will look like
Now pop out 5
Compare 5 with the minimum stack top element 5 is big so don’t remove 4 from the other stack
Now pop out 4
as 4 is available in the minimum stack remove 4 from both the stack and so on.