Frage im Vorstellungsgespräch bei Capital One

Implement a function for determining the validity of a string. The string is valid if parentheses are correctly distributed within the string.

Antworten zu Vorstellungsgespräch

Anonym

27. Feb. 2016

Most of your answers fail. You only count to make sure the number of left parentheses equals the number of right parentheses. You don't check if they are valid. Example ')()(' would pass your checks when it should fail.

6

Anonym

13. Feb. 2017

public static boolean check(String s){ int openParen = 0; for(int i = 0; i < s.length(); i++){ if(s.charAt(i) == '('){ openParen++; } if(s.charAt(i) == ')'){ if(openParen == 0){ return false; } openParen--; } } return openParen == 0; }

1

Anonym

1. Mai 2016

#include using namespace std; int main() { int l=0; int r=0; string str = ""; for(char& c : str) { if(c=='(') l++; else if(c==')' && r!=l) r++; else { cout << "invalid string" << endl; return 0; } } if(l==r)cout << "valid string" << endl; }

1

Anonym

9. Juli 2016

public boolean isBalanced(String text) { int left = 0; int right = 0; for (int i = 0; i left) { return false; } } return (left == right); }

Anonym

9. Juli 2016

public boolean isBalanced(String text) { int left = 0; int right = 0; for (int i = 0; i left) { return false; } } return (left == right); }

Anonym

15. Nov. 2015

def iterator(string): length = len(string) listOfIndices = [] for i in range(length): if string[i] == '(': stillValid = findMatch(i, string, listOfIndices) if not stillValid: return False for i in range(length): if string[i] == ')': if i not in listOfIndices: return False return True def findMatch(currIndex, string, listOfIndices): length = len(string) if currIndex == length -1: return False for i in range(currIndex+1,length): if i not in listOfIndices: if string[i]==')': listOfIndices.append(i) return True return False def main(): string = "(())(" print iterator(string) string = "(" print iterator(string) string = "hey)" print iterator(string) string = ")()()()(" print iterator(string) string = "(()()(mnmn)00)" #should be valid print iterator(string)

2

Anonym

19. Nov. 2015

#return true for valid, false for invalid def validParens(string): length = len(string) left = 0 right = 0 for i in range(length): if string[i] == "(": left += 1 elif string[i] == ")": right += 1 if right > left: return False return True

1

Anonym

9. Nov. 2015

Public Boolean check(string str) { Int left_count = 0; Int right_count = 0; For(int x =0; x

3