Aricent Interview Question
Software EngineersCountry: India
Interview Type: Written Test
/* IMPORTANT: Multiple classes and nested static classes are supported */
// uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import for Scanner and other utility classes
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
/*
* Read input from stdin and provide input before running
* Use either of these methods for input
*/
//BufferedReader
Scanner s = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
n = s.nextInt();
HashMap<String,Integer> hm=new HashMap<String,Integer>();
int var = 0;
for(int i = 0; i < n; i++)
{
String line = br.readLine();
int ind = line.indexOf("=");
if(ind == -1)
{
continue;
}
// System.out.println("ind = " + ind);
String temp = line.substring(0,ind-1);
// System.out.println("temp = " + temp);
if(hm.get(temp) == null)
{
hm.put(temp, 1);
var++;
}
// int N = Integer.parseInt(line);
//Scanner
}
System.out.println(var);
}
}
/*2
foo = 3;
bar = 4;
*/
/* IMPORTANT: Multiple classes and nested static classes are supported */
// uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import for Scanner and other utility classes
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
/*
* Read input from stdin and provide input before running
* Use either of these methods for input
*/
//BufferedReader
Scanner s = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
n = s.nextInt();
HashMap<String,Integer> hm=new HashMap<String,Integer>();
int var = 0;
for(int i = 0; i < n; i++)
{
String line = br.readLine();
int ind = line.indexOf("=");
if(ind == -1)
{
continue;
}
// System.out.println("ind = " + ind);
String temp = line.substring(0,ind-1);
// System.out.println("temp = " + temp);
if(hm.get(temp) == null)
{
hm.put(temp, 1);
var++;
}
// int N = Integer.parseInt(line);
//Scanner
}
System.out.println(var);
}
}
/*2
foo = 3;
bar = 4;
*/
The question is ill posed - with the definition of identifier being a string.
Yes, identifiers are a special string - but not any string. One should formulate the problem as token matching using regex.
<ID> := [a-zA-Z_][a-zA-Z_0-9]*
assignment -> ID '=' [0-9]+ ';'
and we are good. Clearly in this form, we can make the assignment itself a regex and can use named patterns and that is the answer.
something along the lines of the following could work as a simple linux command. not sure this is 100% accurate but the idea is you search for all alphanumeric strings of size 1 or more followed by an equal sign separated by 0 or more spaces, remove the equals sign and subsequent white space, sort by unique values and count the remaining lines. may not be what the interviewer is looking for but at very least worth mentioning.
grep '[a-zA-Z0-9]+ *=' | sed %'='%''% | trim | sort -u | wc -l
var fs=require('fs');
fs.readFile(__dirname + '/' +'testVariable.js','utf-8',(err,data)=>{
if(err){
console.log("Err :",err);
}
data=data.split(';');
var res=[];
for(let i=0;i<data.length;i++){
let str=data[i].split('=')[0]
str=str.replace(new RegExp("var", 'g'), '').trim();
if(str){
res.push(str);
}
}
console.log("res :",res);
});
/* IMPORTANT: Multiple classes and nested static classes are supported */
- chanfool21 February 19, 2017// uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import for Scanner and other utility classes
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
/*
* Read input from stdin and provide input before running
* Use either of these methods for input
*/
//BufferedReader
Scanner s = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
n = s.nextInt();
HashMap<String,Integer> hm=new HashMap<String,Integer>();
int var = 0;
for(int i = 0; i < n; i++)
{
String line = br.readLine();
int ind = line.indexOf("=");
if(ind == -1)
{
continue;
}
// System.out.println("ind = " + ind);
String temp = line.substring(0,ind-1);
// System.out.println("temp = " + temp);
if(hm.get(temp) == null)
{
hm.put(temp, 1);
var++;
}
// int N = Integer.parseInt(line);
//Scanner
}
System.out.println(var);
}
}
/*2
foo = 3;
bar = 4;
*/