count me too
jerana
@jerana
Posts made by jerana

RE: Circular Counter
Here is Go implementation :
func circularCounter(list []int, skip int) {
skip = skip  1 //index is 0
idx := 0
for len(list) > 0 {
idx = (idx + skip) % len(list)
fmt.Printf("%d ", list[idx])
list = append(list[:idx], list[idx+1:]...)
}
return
} 
RE: Uber find the largest kth element in a data stream
Using Min heap with each node record it position in stream . Don't understand why Use Hash with Heap . Use Heap with record arrival index into heap as information should be sufficient

RE: Find the root of a binary tree in a list of tree nodes
type tree struct {
key int
left, right *tree
}var treeNodeList []*tree
func returnRootNodesfromlist(tlist []*tree) *tree {
//Built Map ;
copyLMap := make(map[*tree]bool, 0)
//Call copy inbuilt function
for _, n := range tlist {
copyLMap[n] = true
}
//till hear , builtup Map
//Now Walk to list and remove node right and left node from MAp
for _, n := range tlist {
if n.left != nil {
delete(copyLMap, n.left)
}
if n.right != nil {
delete(copyLMap, n.right)
}
}
if len(copyLMap) > 0 {
for _, n := range copyLMap {
return n
}
}
return nil
} 
Use each character as pivot and check for palindrome in both direction. Use HashMap to save unique Palanidrom
Re: [Given a string of lowercase ASCII characters](find all distinct continuous palindromic substrings of it.)
Here is another solution using pivot each character of string and expend both direction to collect its palindrome part for both even and odd number, Use HashMap to save unique palindrome
/*
Given a string of lowercase ASCII characters, find all distinct continuous palindromic substrings of it.
/
/
Algo : Iterate over each character and take it as pivot and expend its in bothdirection to found if string become palindrom or not . Use hashMap to find
unique palindrom*/
func findSetofPalaindrom(s []byte) {
result := make(map[string]bool, 0)for i := 0; i < len(s); i++ { //Even length palindrom isPalindrom(result, s, i, i+1) //Odd length Palaindron isPalindrom(result, s, i, i) } for v, _ := range result { fmt.Println(v) }
}
func isPalindrom(r map[string]bool, s []byte, i, j int) {
for i >= 0 && j < len(s) && (s[i] == s[j]) {
s1 := string(s[i : j+1])
_, ok := r[s1]
if !ok {
r[s1] = true
}
i
j++
}
} 
RE: Print out an imutable singly linked list in reverse in linear time (O(n)) and less than linear space (space<(O(n))
@vincewu
My Given solution should do it O(n) with O(1) space .
Here is Code which written in Go. Logic it iterate list and reverse its pair nodes. i.e 123 4
iter 0: 2134 , iter1 : 3214, iter3: 4321reverseList(L *list) {
dummy = new(list)
dummy.next = L
cur := L
for( cur != nil && cur.next != nil ) {
tmp := cur.next
next := tmp.next
tmp.next = dummy.next
cur.next = next
dummy.next = tmp
}
cur := dummy.next
for (cur !nil) {
fmt.Println(cur.val)
} 
RE: Print out an imutable singly linked list in reverse in linear time (O(n)) and less than linear space (space<(O(n))
'''
reverseList(L *list) {
dummy = new(list)
dummy.next = L
cur := L
for( cur != nil && cur.next != nil ) {
tmp := cur.next
next := tmp.next
tmp.next = dummy.next
cur.next = next
dummy.next = tmp
}
cur := dummy.next
for (cur !nil) {
fmt.Println(cur.val)
} 
What about if reverse the list first inline then print it . Here is my Golang based code
Re: Print out an imutable singly linked list in reverse in linear time (O(n)) and less than linear space (space<(O(n))
type llist struct {
val int
next *llist
}
func reverseLinkList(L *llist) {
dummy := new(llist)
dummy.next := L
cur := L
for ( cur !=nil && cur.next != nil) {
tmp := cur.next
next := tmp.next
tmp.next = dummy.next
cur.next = next
dummy.next = tmp
}
//After reverse list
cur := dummy.next
for ( cur != nil) {
fmt.Println(cur.val)
}
fmt.Println(}