views:

39

answers:

2

One Interview question i was asked in an interview and i was not get an solution.

Following is the sample paragraph and our program should give the hyperlinks for the words which mathes

"Network protocol Driver- A net-protocol fully Java technology-enabled driver translates JDBC API calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all of its Java technology-based clients to many different databases. The specific protocol used depends on the vendor. In general, this is the most flexible JDBC API alternative. It is likely that all vendors of this solution will provide products suitable for Intranet use. In order for these products to also support Internet access they must handle the additional requirements for security, access through firewalls, etc., that the Web imposes. Several vendors are adding JDBC technology-based drivers to their existing database middleware products"

The words for which we need to give hyperlinks are available in a database table say (URL Table)

Network protocol : www.network-protocol.com

net protocol: www.netprotocol.com

Question is how do you go about updating the links for the above 2 words in the paragraph?

I said I will proceed with the matching word by word and update the links.

If it was a single word matching and updating the url, we can defnitely do with this. but the length of words in the table may be 2 or 3 or 4 Max. then Matching doesnot work and we need to match exactly 2 words for example network protocol and net protocol.

For example if I am searching for protocol word there are many which are matching and chances updating wrong url will be more.

What is the best solution and is there any api availables to do this? If we can do with more efficient as well.

+1  A: 
  1. Read the full paragraph into a String, i.e. str
  2. str.replaceAll(phraseFromDB, "<a href='" + urlFromDB + "'>" + phraseFromDB + "</a>") in a loop based on number of records found in DB table.
Adeel Ansari
if the table has some 800 entries, will this work?
harigm
@harigm: Now it will, I believe. :)
Adeel Ansari
+1  A: 

Here's something I came to think of:

hypothetically speaking; for an arbitrary number of entries in the DB, and an arbitrary length of text (read: many paragraphs, perhaps even pages), matching could be done by building a that holds phrases in the DB and searching on the tree structure.

for example assume that database hold the following phrases:

Network protocol

net protocol

apples and pears

Network awesomeness

Net is teh shit

...

your tree would then have Network, net, apples on the top level with Network and net holding subnodes protocol, awesomeness and protocol, is ... respectively.

I am not sure if this is what you are looking for though, wrote this only because you mentioned that it was a theoretical question at an interview :)

posdef
@posdef, You mean to say use of tree collection help me in coming out with an solution??
harigm
Well what I mean is given a problem like that where regular matching might not work, one could utilize a tree structure. But obviously if the problem at hand can be solved by simple means, mentioned by @Adeel, this would be a clear overkill
posdef