Fragen im Vorstellungsgespräch bei Zalando: Write a method which return h... | Glassdoor.de

Frage im Vorstellungsgespräch

Software Engineer-Vorstellungsgespräch Berlin

Write a method which return how many palindromes are in a

  word. Eg: barbarabar has 7 (b,a,r,barab,a,r)
Antwort

Antwort im Vorstellungsgespräch

6 Antworten

1

import java.util.HashSet;
import java.util.Set;

public class One {

    public static void main(String[] args) {
        System.out.println(palindromeTest("barbarabar"));
    }

    public static int palindromeTest(String string) {
        Set palindromes = new HashSet();
        boolean canBePalindrome;

        for (int i = 0; i = 0 && i + offset < string.length() && canBePalindrome) {
                if (string.charAt(i - offset) == string.charAt(i + offset)) {
                    palindromes.add(string.substring(i - offset, i + offset + 1));
                    offset++;
                } else {
                    canBePalindrome = false;
                }
            }
        }
        return palindromes.size();
    }
}

How much time was given for this task?

Andrey am 26.01.2016
0

int solution(String X) {
      int palCount = 0;
      int singlePal = X.length();

      for (int i = 0; i < X.length(); i++) {
         String sub = "" + X.charAt(i);
         boolean isPal = true;
         for (int j = i + 1; j < X.length(); j++) {
            sub += X.charAt(j);
            if (X.charAt(i) == X.charAt(j))
            {
               // check if a palindrome
               for (int k = 0, l = sub.length() - 1; k < sub.length() / 2; k++, l--) {
                  if (sub.charAt(k) != sub.charAt(l)) {
                     isPal = false;
                     break;
                  }
               }

               if (isPal) {
                  palCount++;
                  singlePal -= sub.length();
                  i = j;
               }

               break;
            }
         }
      }

      return palCount + singlePal;
   }

Anonym am 25.05.2016
1

Did anyone get asked to do this in JavaScript?

Anonym am 20.07.2016
0

(function() {

'use strict'

    var string = "barbarabar",
        _pandulumsFound = [];

    for(var idx = 0, leni = string.length; idx < leni; idx++) {
        var str = string[idx];

        if(_pandulumsFound.indexOf(string[idx]) < 0) {
            _pandulumsFound.push(string[idx]);
        }

        for(var j = idx + 1; j < leni; j++) {

            str += string[j];
            var _isValidPalindrome = true;
            for(var k = 0, lenk = str.length, lenkitr = Math.ceil(lenk / 2); k < lenkitr; k++) {
                if(str[k] != str[lenk - k - 1]) {
                    _isValidPalindrome = false;
                    break;
                }
            }

            _isValidPalindrome && _pandulumsFound.push(str);
        }
    }

    console.log("Pandolums found: " + _pandulumsFound);
})();

annonymous am 14.08.2016
1

def isPalindrome[A](xs: List[A]): Boolean = xs match {
     case (head :: tail) :+ last => if (head == last) isPalindrome(tail) else false
     case Nil | _ :: Nil => true
}

def howManyPalindrome(str: String): Int = {
     def aux(x$1: String, end: Int, cnt: Int): Int = {
          if (x$1.isEmpty) return cnt
          val head = x$1.head
          val lastInd = x$1.lastIndexOf(head, end)
          val x$2 = x$1.slice(0, lastInd + 1)
          if (isPalindrome(x$2.toList)) {
               val x$3 = x$1.replaceFirst(x$2, "")
               aux(x$3, x$3.length, cnt + 1)
          } else aux(x$1, lastInd - 1, cnt)
     }
     aux(str, str.length, 0)
}

Scala Implementation am 14.08.2018
0

function palindrome(str){
  const arr = [];
  for(let i = 0; i < str.length; i++){
    for(let j = i + 1; j <= str.length; j++){
      const word = str.slice(i, j);
      const isPalindrome = word === word.split('').reverse().join('');
      if(isPalindrome) arr.push(word);
    }
  }
  return Array.from(new Set(arr));
}

Anonym am 10.04.2020

Antwort oder Kommentar posten

Um dies zu kommentieren, bitte anmelden oder Konto anlegen.