OLAP Vision Interview Question for Interns


Country: Canada
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

package com.cracking.olap_vision;

public class DecodeString {
	
	public static void main(String[] args) {
		System.out.printf("String = %s , Value = %d\n","A",Decode("A"));
		
		System.out.printf("String = %s , Value = %d\n","AA",Decode("AA"));
		System.out.printf("String = %s , Value = %d\n","AZ",Decode("AZ"));
		
		System.out.printf("String = %s , Value = %d\n","BA",Decode("BA"));
		System.out.printf("String = %s , Value = %d\n","BZ",Decode("BZ"));
		
		System.out.printf("String = %s , Value = %d\n","CA",Decode("CA"));
		System.out.printf("String = %s , Value = %d\n","AAA",Decode("AAA"));
		System.out.printf("String = %s , Value = %d\n","AAZ",Decode("AAZ"));
		
		System.out.printf("String = %s , Value = %d\n","ABA",Decode("ABA"));
		System.out.printf("String = %s , Value = %d\n","ABZ",Decode("ABZ"));
	}
	
	public static int Decode(String str) {
		
		final int interval = 26;
		char[] arr = str.toCharArray();
		int len = arr.length;
		int sum = 0;
		
		for(int posInterval=0, i=len-1; i>=0; i--,posInterval++) {
			char ch = arr[i];
			int value = (ch - 'A') +1;
			value *= Math.pow(interval, posInterval);
			sum += value;
		}
		return sum;
	}

}

Output:
String = A , Value = 1
String = AA , Value = 27
String = AZ , Value = 52
String = BA , Value = 53
String = BZ , Value = 78
String = CA , Value = 79
String = AAA , Value = 703
String = AAZ , Value = 728
String = ABA , Value = 729
String = ABZ , Value = 754

- ProTechMulti October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Known problem, and here is the solution.
[ codereview.stackexchange.com/questions/44545/excel-column-string-to-row-number-and-vice-versa ]

- NoOne October 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using simple number system it can be done .
BA -> 26x2+1 =53
BC ->26x2+3 =55
BCC-> 26^2x2+26^1x3+26^0x3

- Afsar October 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
  	calc("BAC");
  }
 
  public static void calc(String str){
  	int n = str.length()-1;
    char[] carr = str.toCharArray();
    
    int sum = carr[n] - 'A' +1;
    n--;
    int i = 1;
    while(n >= 0){
 		sum += (carr[n] - 'A' + 1)*(int)Math.pow(26, i);
      	n--;
      	i++;
    }
    System.out.println(sum);
  }

- sudip.innovates October 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

JS

function getValue(str) {

	var sum = 0;

	for (var i = 0; i < str.length; i++) {
  
  	var c = str.charAt(i);
    
    if (c >= 'A' && c <= 'Z') {
    	sum += Math.pow(26, str.length - i - 1) * (c.charCodeAt(0) - "A".charCodeAt(0) + 1)
    } else {
    	console.error("Invalid input!");
    }
  }
	
  return sum;
}

- Kevin Connors October 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int Decode(string const &s)
{
	int val = 1;
	int n = 0;
	for (int i = s.size() - 1; i >= 0; --i) {
		n += val * (s[i] - 'A' + 1);
		val *= 26;
	}
	return n;
}

- Alex October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findValue(s):
    v=0
    for c in s:
        v = 26* v+ 1 +ord (c) - ord ('A')
    return v

- Makarand October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class BaseAlphabet {
    final List ALPHABET = 'A'..'Z'
    final Map MAP = ALPHABET.withIndex(1).collectEntries { k, v -> [(k): v] }
    
    int getBase10(String s) {
        s.toUpperCase().findAll { it }
            .collect { MAP[it] }
            .inject(0) { acc, val -> acc * 26 + val}
    }
}

Map tests = [
    'A': 1,
    'B': 2,
    'Z': 26,
    'AA': 27,
    'AB': 28,
    'AZ': 52,
    'BA': 53,
    'CC': 81,
    'cafe': 53565,
]
def ba = new BaseAlphabet()
tests.collect { k, v ->
    def actual = ba.getBase10(k)
    println "getBase10(${k}) = ${actual} \t(${actual == v})"
    actual == v
}.any { !it }

getBase10(A) = 1 (true)
getBase10(B) = 2 (true)
getBase10(Z) = 26 (true)
getBase10(AA) = 27 (true)
getBase10(AB) = 28 (true)
getBase10(AZ) = 52 (true)
getBase10(BA) = 53 (true)
getBase10(CC) = 81 (true)
getBase10(cafe) = 53565 (true)

- Brian.C.Street October 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findValue(s):
    v = 0

    b = range(len(s))
    b.reverse()

    c = zip(s,b)

    for char, val in c:
        f = ord(char) - ord('A') + 1
        v = 26 ** val * f + v

    return v

t_l = {"A": 1, "AZ": 52, "CA": 79, "AAZ": 728, "ABZ": 754}


for k,v in t_l.items():
    print ("{}: {}".format(k, v))
    assert(findValue(k) == v)

- Anonymous October 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi

- Anonymous October 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Function in C

#include <stdio.h>
#include <string.h>

long stringtonum(char* str)
{
	int i=strlen(str)-1;
	long sum=0, mul = 1;

	while(i >= 0)
	{
		sum += mul*(1+str[i]-'A');
		i--; mul *= 26;
	}

	return sum;
}

- slimved3 October 30, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php
$input = 'AAAA';
$inputArray = str_split($input);
$length = count($inputArray);
$result = 0;

foreach ($inputArray as $key => $val) {
    $pow = $length - ($key + 1);
    $result += pow(26, $pow) * getNumber($val);
}

echo "The result is " . $result;

function getNumber($char) {
    return ord($char) - 64;
}

- Anonymous September 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php
$input = 'AAAA';
$inputArray = str_split($input);
$length = count($inputArray);
$result = 0;

foreach ($inputArray as $key => $val) {
    $pow = $length - ($key + 1);
    $result += pow(26, $pow) * getNumber($val);
}

echo "The result is " . $result;

function getNumber($char) {
    return ord($char) - 64;
}

- Tibin Paul September 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<?php
$input = 'AAAA';
$inputArray = str_split($input);
$length = count($inputArray);
$result = 0;

foreach ($inputArray as $key => $val) {
    $pow = $length - ($key + 1);
    $result += pow(26, $pow) * getNumber($val);
}

echo "The result is " . $result;

function getNumber($char) {
    return ord($char) - 64;

}

- Tibin Paul September 15, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def findValue(s):
v=0
for p,x in enumerate(reversed(s)):
v+=26**p*(ord(x)-ord('A')+1)
return v

- AMITH B January 17, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More