Last active
April 13, 2016 05:33
-
-
Save zeeshankhan/80d36ff9c32809a63639 to your computer and use it in GitHub Desktop.
Interview Coding Questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check If a string has all unique characters, without using additional data structure. | |
func isUnique(string: String) -> Bool { | |
// what type of characters // if ascii characters | |
if string.characters.count > 128 { | |
return true | |
} | |
var cIdx = 0 | |
for c in string.characters { | |
var ccIdx = 0 | |
for cc in string.characters { | |
if cIdx != ccIdx && c == cc { | |
print("\(c)==\(cc)") | |
return true | |
} | |
ccIdx = ccIdx + 1 | |
} | |
cIdx = cIdx + 1 | |
} | |
return false | |
} | |
print(isUnique("qwertyuiopasdfghjklzxcv1234567890-=!@#$%^&*()_+<>:{}|~`,./;'bnm,.")) | |
// Trim whitespace characters from beginning, end of the string, and more than one space in between. | |
func trimming(string : String) -> String? { | |
var str: String = "" | |
var isCarShuru = false | |
var isMidSpace = false | |
for i in 0..<string.characters.count { | |
let char = Array(string.characters)[i] | |
if char != " " { | |
if isMidSpace { | |
str = str + " " + String(char) | |
isMidSpace = false | |
} | |
else { | |
str = str + String(char) | |
} | |
isCarShuru = true | |
} | |
else { | |
if isCarShuru && isMidSpace == false { | |
isMidSpace = true | |
} | |
} | |
} | |
return str | |
} | |
print(trimming(" Roch the world ")) | |
// Check Palindrome or a permutation of palindrome | |
func isPalindrome(string: String) -> Bool { | |
var oddCount = 0 | |
var arrDone = Array(count: string.characters.count, repeatedValue: "Yo") | |
for c1 in string.characters { | |
var isSame = false | |
for cOld in arrDone { | |
if cOld == String(c1) { | |
isSame = true | |
} | |
} | |
if !isSame { | |
var isOdd = false | |
for c2 in string.characters { | |
if c1 == c2 { | |
isOdd = !isOdd | |
} | |
} | |
if isOdd { | |
oddCount = oddCount + 1 | |
if oddCount == 2 { | |
return false; | |
} | |
} | |
arrDone.append(String(c1)) | |
} | |
} | |
return true | |
} | |
print(isPalindrome("aibohphobia")) | |
print(isPalindrome("palindrome")) | |
// Check Permutation: given two string, if one is a permutation to other. | |
func isPermutation(string1: String, of string2: String) -> Bool { | |
if (string1.characters.count != string2.characters.count) { | |
return false | |
} | |
var charArray = Array(count: string1.characters.count, repeatedValue: "Yo") | |
for char1 in string1.characters { | |
var isFound = false | |
for c0 in charArray { | |
if String(char1) == c0 { | |
isFound = true | |
break | |
} | |
} | |
if !isFound { | |
var char1Count = 0 | |
for c in string1.characters { | |
if c == char1 { | |
char1Count = char1Count + 1; | |
} | |
} | |
var char2Count = 0 | |
for char2 in string2.characters { | |
if char1 == char2 { | |
char2Count = char2Count + 1; | |
} | |
} | |
print(char1, char1Count, char2Count) | |
if char1Count != char2Count { | |
return false | |
} | |
charArray.append(String(char1)) | |
} | |
} | |
print(charArray) | |
return true | |
} | |
print(isPermutation("yobabymushaya", of: "mushayaabyboy")) | |
// Given a string check if its a Permutation of a palindrome or not. | |
// first it should be a palindrome, then check for permutation. | |
func isPermutationOfAPalindrome(string : String) -> Bool { | |
let cnt = string.characters.count | |
if cnt < 2 { | |
return false | |
} | |
if !isPalindrome(string) { | |
return false | |
} | |
if cnt == 2 { | |
return false | |
} | |
let x = cnt / 2 | |
let arr = Array(string.characters) | |
for i in 0..<x { | |
let c1 = arr[i]; | |
let j = cnt - (i+1) | |
let c2 = arr[j] | |
if c1 != c2 { | |
print(c1, c2) | |
return false | |
} | |
} | |
return true | |
} | |
print(isPermutationOfAPalindrome("levle")) | |
print(isPermutationOfAPalindrome("refer")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment