Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can this Java code be improved to find sub-string in a string?
    text
    copied!<p>I was recently asked to submit a solution to a problem for a job. </p> <p><strong>Problem</strong>: Find a sub-string in a string.</p> <pre><code>Input: "Little star's deep dish pizza sure is fantastic." Search: "deep dish pizza" Output: "Little star's [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]] sure is fantastic." </code></pre> <p>Note that the highlighter doesn't have to have the exact same result on this example, since <em>you</em> are defining what a good snippet is and return the the most relevant snippet with the query terms highlighted.</p> <p>The most important requirement was to write it as I would write a <strong>production code</strong>.</p> <p>My solution was not accepted. How could I have improved it? I know, I could have used:</p> <ol> <li><a href="http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" rel="nofollow">Knuth–Morris–Pratt algorithm</a></li> <li>Regex (could I?)</li> </ol> <p><strong>My QUESTION:</strong> </p> <ol> <li><p>What do tech companies take into consideration when they review a code for a job. I submitted the code the same day, does that help in any way? </p></li> <li><p>In one of the comments, it pointed out, it looks like a school code than production code. How? Any suggestions?</p></li> </ol> <p>My solution:<br> <strong>FindSubString.java</strong></p> <pre><code>/** * FindSubString.java: Find sub-string in a given query * * @author zengr * @version 1.0 */ public class FindSubstring { private static final String startHighlight = "[[HIGHLIGHT]]"; private static final String endHighlight = "[[ENDHIGHLIGHT]]"; /** * Find sub-string in a given query * * @param inputQuery: A string data type (input Query) * @param highlightDoc: A string data type (pattern to match) * @return inputQuery: A String data type. */ public String findSubstringInQuery(String inputQuery, String highlightDoc) { try { highlightDoc = highlightDoc.trim(); if (inputQuery.toLowerCase().indexOf(highlightDoc.toLowerCase()) &gt;= 0) { // update query if exact doc exists inputQuery = updateString(inputQuery, highlightDoc); } else { // If exact doc is not in the query then break it up String[] docArray = highlightDoc.split(" "); for (int i = 0; i &lt; docArray.length; i++) { if (inputQuery.toLowerCase().indexOf(docArray[i].toLowerCase()) &gt; 0) { inputQuery = updateString(inputQuery, docArray[i]); } } } } catch (NullPointerException ex) { // Ideally log this exception System.out.println("Null pointer exception caught: " + ex.toString()); } return inputQuery; } /** * Update the query with the highlighted doc * * @param inputQuery: A String data type (Query to update) * @param highlightDoc: A String data type (pattern around which to update) * @return inputQuery: A String data type. */ private String updateString(String inputQuery, String highlightDoc) { int startIndex = 0; int endIndex = 0; String lowerCaseDoc = highlightDoc.toLowerCase(); String lowerCaseQuery = inputQuery.toLowerCase(); // get index of the words to highlight startIndex = lowerCaseQuery.indexOf(lowerCaseDoc); endIndex = lowerCaseDoc.length() + startIndex; // Get the highlighted doc String resultHighlightDoc = highlightString(highlightDoc); // Update the original query return inputQuery = inputQuery.substring(0, startIndex - 1) + resultHighlightDoc + inputQuery.substring(endIndex, inputQuery.length()); } /** * Highlight the doc * * @param inputString: A string data type (value to be highlighted) * @return highlightedString: A String data type. */ private String highlightString(String inputString) { String highlightedString = null; highlightedString = " " + startHighlight + inputString + endHighlight; return highlightedString; } } </code></pre> <p><strong>TestClass.java</strong></p> <pre><code>/** * TestClass.java: jUnit test class to test FindSubString.java * * @author zengr * @version 1.0 */ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestClass extends TestCase { private FindSubstring simpleObj = null; private String originalQuery = "I like fish. Little star's deep dish pizza sure is fantastic. Dogs are funny."; public TestClass(String name) { super(name); } public void setUp() { simpleObj = new FindSubstring(); } public static Test suite(){ TestSuite suite = new TestSuite(); suite.addTest(new TestClass("findSubstringtNameCorrect1Test")); suite.addTest(new TestClass("findSubstringtNameCorrect2Test")); suite.addTest(new TestClass("findSubstringtNameCorrect3Test")); suite.addTest(new TestClass("findSubstringtNameIncorrect1Test")); suite.addTest(new TestClass("findSubstringtNameNullTest")); return suite; } public void findSubstringtNameCorrect1Test() throws Exception { String expectedOutput = "I like fish. Little star's deep [[HIGHLIGHT]]dish pizza[[ENDHIGHLIGHT]] sure is fantastic. Dogs are funny."; assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, "dish pizza")); } public void findSubstringtNameCorrect2Test() throws Exception { String expectedOutput = "I like fish. Little star's [[HIGHLIGHT]]deep dish pizza[[ENDHIGHLIGHT]] sure is fantastic. Dogs are funny."; assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, "deep dish pizza")); } public void findSubstringtNameCorrect3Test() throws Exception { String expectedOutput = "Hello [[HIGHLIGHT]]how[[ENDHIGHLIGHT]] are [[HIGHLIGHT]]you[[ENDHIGHLIGHT]]r?"; assertEquals(expectedOutput, simpleObj.findSubstringInQuery("Hello how are your?", "how you")); } public void findSubstringtNameIncorrect1Test() throws Exception { String expectedOutput = "I like fish. Little star's deep dish pizza sure is fantastic. Dogs are funny."; assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, "I love Ruby too")); } public void findSubstringtNameNullTest() throws Exception { String expectedOutput = "I like fish. Little star's deep dish pizza sure is fantastic. Dogs are funny."; assertEquals(expectedOutput, simpleObj.findSubstringInQuery(originalQuery, null)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload