Find the score for each number, you got points for:
+1 for number divisible by 7
+2 for even digits
+4 for each 9
+5 for 2 consecutive 1 and +5 for each consecutive followine one
+N2 for every continous sequnce of the form 123 or 4567 of length N
e.g. If the number is 11129 you get:
- 5+5 points for conseutive 1s,
- 2 points for an even digit(2)
- 1+1+2^2+1 for the increasing sequence of lengths 1,1,2, 1
- 0 points for divisibility by 7
- 4 points for the 9
// Complete the compute_number_score function below.
static int compute_number_score(int number) {
if(number==0){
return 2;
}
// System.out.println( getDigitWiseScore(number) + " " + countConsecutiveOnes(number) +" " +get(number));
return getDigitWiseScore(number) + countConsecutiveOnes(number) +get(number);
}
public static int getDigitWiseScore(int num){
int score = 0;
if(num%7==0) score+=1;
while(num>0){
int d = num%10;
if(d==9) score+=4;
if(d%2==0) score+=2;
num = num/10;
}
return score;
}
public static int countConsecutiveOnes(int num){
int score =0;
int currentOnes = 0;
while(num > 0 ){
int d = num%10;
num = num/10;
if(d!=1 && currentOnes > 1 ){
score+=5;
score += (currentOnes-2)*5;
currentOnes = 0;
}
if(d==1){
currentOnes++;
}
if(d!=1 && currentOnes == 1 ){
currentOnes = 0;
}
}
if(currentOnes> 1){
score+=5;
score += (currentOnes-2)*5;
}
return score;
}
public static int getSeq(int num){
String s = Integer.toString(num);
int score=0;
int pointer = 1;
int current = 1;
int previous = s.charAt(0);
while(pointer< s.length()){
if(s.charAt(pointer) == previous){
current++;
previous++;
} else if( current > 1 && s.charAt(pointer) != previous+1 ){
score = score+ (current*current);
current =01;
score = score+1;
} else{
score = score+1;
}
previous = s.charAt(pointer);
pointer++;
}
return score;
}
public static int get(int num) {
int x=0, count=1;
List<Integer> store = new ArrayList<>();
String s = Integer.toString(num);
while(x<s.length()-1) {
if (s.charAt(x)+1 == s.charAt(x+1)){
count ++;
x++;
}
else {
x++;
store.add(count);
count = 1;
}
}
store.add(count);
int score = 0;
for(Integer i: store){
score += i*i;
}
return score;
}
Find the score for each number, you got points for:
+2 for number divisible by 3
+4 for even digits
+1 for each 7
+3 for 2 consecutive 5s and +3 for each consecutive following Five
+N2 for every continuous decreasing sequence of the form 321 or 765 of length N
static int compute_number_score(int number) {
return getDigitWiseScore(number) + countConsecutiveFives(number) + get(number);
}
public static int getDigitWiseScore(int num) {
int score = 0;
if (num % 3 == 0) score += 2;
while (num > 0) {
int d = num % 10;
if (d == 7) score += 1;
if (d % 2 == 0) score += 4;
num = num / 10;
}
return score;
}
public static int countConsecutiveFives(int num) {
int score = 0;
int currentOnes = 0;
while (num > 0) {
int d = num % 10;
num = num / 10;
if (d != 5 && currentOnes > 1) {
score += 3;
score += (currentOnes - 2) * 3;
currentOnes = 0;
}
if (d != 5 && currentOnes == 1) {
currentOnes = 0;
}
if (d == 5) {
currentOnes++;
}
}
if (currentOnes > 1) {
score += 3;
score += (currentOnes - 2) * 3;
}
return score;
}
public static int get(int num) {
int x = 0, count = 1;
List < Integer > store = new ArrayList < > ();
String s = Integer.toString(num);
while (x < s.length() - 1) {
if (s.charAt(x) == s.charAt(x + 1) + 1) {
count++;
x++;
} else {
x++;
store.add(count);
count = 1;
}
}
store.add(count);
int score = 0;
for (Integer i: store) {
score += i * i;
}
return score;
}
- Given a binary tree of routers, if one node fails - all its descendents become unreachable. 3 MCQs about: you choose a random node what is the probability of being able to reach that node, what nodes are most likely to fail etc.
Coding Question 1:
The question was about checking if the given sequence of events is valid. The conditions are:
The order of release should be reverse of aquiring(stack)
A locked lock can not be aquired again
A lock has to be locked before releasing
There should be no dangling locks
// Complete the check_log_history function below.
static int check_log_history(List<String> events) {
HashSet<Integer> hs = new HashSet();
Stack<Integer> st = new Stack();
for(int i = 0; i < events.size(); i++){
String e = events.get(i);
String name = e.split(" ")[0];
int num = Integer.parseInt(e.split(" ")[1]);
if(name.equals("ACQUIRE")){
if(hs.contains(num)){
return i+1;
}
st.push(num);
hs.add(num);
}else{
if(!hs.contains(num) || st.peek() != num){
return i+1;
}
st.pop();
hs.remove(num);
}
}
return st.empty() ? 0 : events.size()+1;
}
Coding Question 2:
Number Score:
V1: https://leetcode.com/discuss/interview-question/390058/pure-storage-oa-2019-number-score
V2: https://leetcode.com/discuss/interview-question/406130/Pure-Storage-or-OA-2019-or-Number-Score-V2