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
Sample output

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;
if (unique) {
System.out.println(“First non repeated character in String \””
+ s + “\” is:” + s.charAt(i));
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”}

// 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 =;

// If this is first occurrence of word
hm.put(word, 1);
// 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();

String w =;

if (hm.get(w) == 1)

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


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

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());


// 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

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