TalkIQ Interview Question
Senior Software Development EngineersCountry: Canada
Interview Type: In-Person
import java.util.Arrays;
import java.util.List;
/**
*
* @author vensi
*/
public class CountDigitList {
public static void main(String[] args) {
// Conventional way
List<Integer> l = Arrays.asList(0, 100, 20, 44, 56, 64, 79);
int k = 4;
int totalCount = 0;
for (int i : l) {
totalCount+=countDigitInNumber(k,i);
}
System.out.println(totalCount);
//Using java 8 streams and Strings
String s = l.parallelStream().map(a->a.toString()).reduce("",(a,b)->a.concat(b));
System.out.println(s.length() - s.replace(k+"", "").length());
}
private static int countDigitInNumber(int digit, int number) {
int count = 0;
do {
int remainder = number % 10;
number = number / 10;
if (digit == remainder) {
count++;
}
} while (number > 0);
return count;
}
}
import java.util.*;
public class TestDemo
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n = input.nextInt(); //total numbers in the list L
int k = input.nextInt(); //the value to count
String[] array = new String[n]; //list of numbers goes here
String totalString = " ";
for(int i = 0;i<n;i++)
{
array[i]=input.next();
totalString = totalString + array[i];
}
//now you have a string for all the list numbers
char[] arrayOfTotalString = totalString.toCharArray();
int count = 0;
for(int i = 0;i<totalString.length();i++)
{
if(k==Character.getNumericValue(arrayOfTotalString[i]))
{
count++;
}
}
System.out.println(count); //final count
}
}
}
import java.util.*;
public class TestDemo
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n = input.nextInt(); //total numbers in the list L
int k = input.nextInt(); //the value to count
String[] array = new String[n]; //list of numbers goes here
String totalString = " ";
for(int i = 0;i<n;i++)
{
array[i]=input.next();
totalString = totalString + array[i];
}
//now you have a string for all the list numbers
char[] arrayOfTotalString = totalString.toCharArray();
int count = 0;
for(int i = 0;i<totalString.length();i++)
{
if(k==Character.getNumericValue(arrayOfTotalString[i]))
{
count++;
}
}
System.out.println(count); //final count
}
}
public class AppTest
{
public static void main(String[] args)
{
int[] k = {0, 3, 9};
int[] l = {0, 45, 3900, 9};
for (int ek : k)
System.out.println(ek + " : " + howmany(l, ek));
}
public static int howmany(int[] L, int k)
{
int total = 0;
for (int l: L) {
if (l == 0 && l == k)
total++;
if (l > 0) {
String strL = String.valueOf(l);
for (int i = 0; i < strL.length(); i++) {
if (Character.getNumericValue(strL.charAt(i)) == k)
total++;
}
}
}
return total;
}
}
Unfortunately I have only relatively slow O(list.length * digits in each list member) solution to the problem. Inside my algorithm there is a for loop that for every number in a collection calls another function countDigit(int n, int digit) that counts how many times digit appears in that number. countDigit is recursive function with O(digits in number) complexity.
import java.util.*;
public class Example{
public static int countDigit(int n, int digit)
{
if(n == digit)return 1;
if(n == 0 && n != digit)return 0;
if(n % 10 == digit)
{
return 1 + countDigit(n/10, digit);
}else
{
return 0 + countDigit(n/10, digit);
}
//for example n = 3242 digit = 2;
//1. 3242 % 10 == 2 so return 1 + countDigit(3242/10, 2)
//2. 324 % 10 != 2 so return 0 + countDigit(324/10, 2)
//3. 32 % 10 == 2 so return 1 + countDigit(32/10, 2);
//4. 3 % 10 != 2 so return 0 + countDigit(3/10, 2);
//5. 0 == base case so return 0;
//countDigit = cD
//cD(3242, 2) = 1 + cD(324, 2); cd(3242, 2) = 1 + 0 + cD(32, 2);
//cd(3242, 2) = 1 + 0 + 1 + cd(3, 2); cd(3242, 2) = 1+0+1+cD(0, 2) = 1+0+1+0 = 2;
}
public static int howMany(List<Integer> list, int digit)
{
int counter = 0;
for(int number : list)
{
counter += countDigit(number, digit);
}
return counter;
}
public static void main(String []args){
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(12);
list.add(245);
list.add(34);
list.add(764);
list.add(2227);
list.add(324262);
int number = 2;
System.out.println("In list there are " + howMany(list, number) + " digits equals " + number);
//In list there are 9 digits equals 2
}
}
Flattening the number list to string and iterating over it.
In Python:
flat_L = ''.join(str, L)
k_occurrences = [0] * 10
for num in flat_L:
k_occurrences[int(num)] += 1
Here L is the initial list with integer values;
k_occurrences - list where index represent the k number; and value represents the occurrences
- smaharj1@ramapo.edu September 06, 2016