## ADF Data Science – Java Interview Questions with Answers

In this post, we are going to discuss about ADF Data Science – Java interview questions with answers.  Yesterday, our Java trainees went to attend ADF Data Science Interview in Chennai.  There are 3 sets of Interview Questions.  The below is one among them.  The questions are mostly testing the logical ability of the candidates.

### 1. How to find the first non-repeated character in a string?

Sample input
applieddatafinance
Sample output
l

ANSWER:
First of all, we should understand the pseudo algorithm and logic of the question which is as follows;

Logic –
A non repeated character occurs only once in the string and so if we store the number of times each alphabet appears in the string, it would help us to identify the non repeated characteres in the string. So if we scan the whole string and determine the final counts of each character, the first character in the string with final count 1 is the first non repeated character in the string.

Pseudo Algorithm –
i) First create the character count hash table.
For each character, if there is no value stored in the cahracter sert it to 1.
Else increment the value of th echaracter by 1.
ii) Scan the string.
For each character return character if the count in hash table is 1.
If no character have count 1, return null.

Code to find the first non-repeated character in a string:

public class NonRepeatedCharacter {
public static void main(String[] args) {
String s = “applieddatafinance”;
for (int i = 0; i < s.length(); i++) {
boolean unique = true;
for (int j = 0; j < s.length(); j++) {
if (i != j && s.charAt(i) == s.charAt(j)) {
unique = false;
break;
}
}
if (unique) {
System.out.println(“First non repeated character in String \””
+ s + “\” is:” + s.charAt(i));
break;
}
}
}
}
Output:
First non repeated character in String “applieddatafinance” is:l

### 2. Write a program to get distinct elements from an array by avaoiding duplicate elements.Ex: String arr[]={“vignesh”,”vinoth”,”vinitha”,”ramesh”,”vignesh”,”vinoth”,”vinitha”}

ANSWER:
// Java program to print unique words
// from a string

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test
{
// Prints unique words in a string
static void printUniquedWords(String str)
{
// Extracting words from string
Pattern p = Pattern.compile(“[a-zA-Z]+”);
Matcher m = p.matcher(str);

// Map to store count of a word
HashMap<String, Integer> hm = new HashMap<>();

// if a word found
while (m.find())
{
String word = m.group();

// If this is first occurrence of word
if(!hm.containsKey(word))
hm.put(word, 1);
else
// increment counter of word
hm.put(word, hm.get(word) + 1);

}

// Traverse map and print all words whose count
// is 1
Set<String> s = hm.keySet();
Iterator<String> itr = s.iterator();

while(itr.hasNext())
{
String w = itr.next();

if (hm.get(w) == 1)
System.out.println(w);
}
}

// Driver Method
public static void main(String[] args)
{
String arr[]={“vignesh”,”vinoth”,”vinitha”,”ramesh”,”vignesh”,”vinoth”,”vinitha”}
printUniquedWords(str);
}
}

Output-
vignesh
vinoth
vinitha
ramesh

### 3. Sort or order a HashMap or any map item by key. Write a comparator which compares by key, not by value.

ANSWER:
A comparator program using ArrayList, which compares by key is as follows-

// Java Code to sort Map by key value
import java.util.*;
class sortmapKey {

// This map stores unsorted values
static Map<String, Integer> map = new HashMap<>();

// Function to sort map by Key
public static void sortbykey()
{
ArrayList<String> sortedKeys =
new ArrayList<String>(map.keySet());

Collections.sort(sortedKeys);

// Display the TreeMap which is naturally sorted
for (String x : sortedKeys)
System.out.println(“Key = ” + x +
“, Value = ” + map.get(x));
}

// Driver Code
public static void main(String args[])
{
// putting values in the Map
map.put(“Jacob”, 80);
map.put(“Abhinaya”, 90);
map.put(“Azaar”, 80);
map.put(“Asadullah”, 75);
map.put(“Dinesh”, 40);

// Calling the function to sortbyKey
sortbykey();
}
}

Output:
Key = Abhinaya, Value = 90
Key = Asadullah, Value = 75
Key = Azaar, Value = 80
Key = Dinesh, Value = 40
Key = Jacob, Value = 80

References:

http://www.javatechblog.com/java/java-program-to-find-first-non-repeated-character-in-a-string/

http://javahungry.blogspot.com/2013/12/first-non-repeated-character-in-string-java-program-code-example.html

https://www.geeksforgeeks.org/print-unique-words-string/

https://www.dreamincode.net/forums/topic/254693-trying-to-print-distinct-numbers-in-an-array/

https://www.geeksforgeeks.org/sorting-hashmap-according-key-value-java/

https://www.mkyong.com/java/how-to-sort-a-map-in-java/