Skip to content

Instantly share code, notes, and snippets.

@youngershen
Last active September 25, 2021 14:47
Show Gist options
  • Save youngershen/30479117c4d8a6eafb83818add71a7f7 to your computer and use it in GitHub Desktop.
Save youngershen/30479117c4d8a6eafb83818add71a7f7 to your computer and use it in GitHub Desktop.
Blockchain Interview quiz 101
Q1
Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Example 4:
Input: num = 1234567891
Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
Constraints:
0 <= num <= 231 - 1
Q2
You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters.
You can return the answer in any order.
Example 1:
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output: []
Example 3:
Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]
Constraints:
1 <= s.length <= 104
s consists of lower-case English letters.
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i] consists of lower-case English letters.
@toints
Copy link

toints commented Sep 25, 2021

A2:

package main

import (
    "fmt"
)

func findStrings(s string, words []string) []int {
    if len(s) == 0 || len(words) == 0 {
        return []int{}
    }

    wordsCnt := make(map[string]int, 0)
    for _, word := range words {
        wordsCnt[word]++
    }

    wordsLen := len(words)
    length := len(words[0])
    result := make([]int, 0)

    for index := 0; index + wordsLen * length <= len(s); index++ {
        ss := s[index : index+length]

        if _, ok := wordsCnt[ss]; ok {
            sCnt := make(map[string]int, 0)
            var j int
            for j = 0; j < wordsLen; j++ {
                temp := s[index + j * length : index + (j + 1) * length]
                sCnt[temp]++
                if sCnt[temp] > wordsCnt[temp] {
                    break
                }
            }
            if j == wordsLen {
                result = append(result, index)
            }
        }
    }
    return result
}

func main() {
    s1 := "barfoothefoobarman"
    words1 := []string{"foo","bar"}
    ret1:= findStrings(s1, words1)
    fmt.Println(ret1)

    s2 := "wordgoodgoodgoodbestword"
    words2:= []string{"word","good","best","word"}
    ret2:= findStrings(s2, words2)
    fmt.Println(ret2)

    s3 := "barfoofoobarthefoobarman"
    words3 := []string{"bar","foo","the"}
    ret3:= findStrings(s3, words3)
    fmt.Println(ret3)
}

@gitslagga
Copy link

A1

package main

import (
	"fmt"
	"math"
	"strconv"
)

func main() {
	// Convert a non-negative integer num to its English words representation.

	// Example 1:

	// Input: num = 123
	// Output: "One Hundred Twenty Three"
	// Example 2:

	// Input: num = 12345
	// Output: "Twelve Thousand Three Hundred Forty Five"
	// Example 3:

	// Input: num = 1234567
	// Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
	// Example 4:

	// Input: num = 1234567891
	// Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

	// Constraints:

	// 0 <= num <= 231 - 1

	NumberConvert(123)
	NumberConvert(12345)
	NumberConvert(1234567891)
}

var oneBit = map[string]string{
	"1": "One",
	"2": "Two",
	"3": "Three",
	"4": "Four",
	"5": "Five",
	"6": "Six",
	"7": "Seven",
	"8": "Eight",
	"9": "Nine",
}

var tenSimBit = map[string]string{
	"1": "Eleven",
	"2": "Twelve",
	"3": "Thirteen",
	"4": "Fourteen",
	"5": "Fifteen",
	"6": "Sixteen",
	"7": "Seventeen",
	"8": "Eighteen",
	"9": "Nineteen",
}

var tenBit = map[string]string{
	"2": "Twenty",
	"3": "Thirty",
	"4": "Fourty",
	"5": "Fifty",
	"6": "Sixty",
	"7": "Seventy",
	"8": "Eighty",
	"9": "Ninety",
}

func NumberConvert(num int) (out string) {
	if 0 <= num && num <= int(math.Pow(2, 31))-1 {
		strNum := strconv.Itoa(num)
		strNum = NumberReverse(strNum)

		for key, val := range strNum {
			switch key {
			case 0:
				out = oneBit[string(val)]
			case 1:
				if string(val) == "1" {
					out = tenSimBit[strNum[0:1]]
				} else {
					out = tenBit[string(val)] + " " + out
				}
			case 2:
				out = oneBit[string(val)] + " Hundred " + out
			case 3:
				if strNum[4:5] != "1" {
					out = oneBit[string(val)] + " Thousand " + out
				}
			case 4:
				if string(val) == "1" {
					out = tenSimBit[strNum[3:4]] + " Thousand " + out
				} else {
					out = tenBit[string(val)] + " " + out
				}
			case 5:
				out = oneBit[string(val)] + " Hundred " + out
			case 6:
				if strNum[7:8] != "1" {
					out = oneBit[string(val)] + " Million " + out
				}
			case 7:
				if string(val) == "1" {
					out = tenSimBit[strNum[6:7]] + " Million " + out
				} else {
					out = tenBit[string(val)] + " " + out
				}
			case 8:
				out = oneBit[string(val)] + " Hundred " + out
			case 9:
				out = oneBit[string(val)] + " Billion " + out
			}
		}
	}

	fmt.Println(out)
	return
}

func NumberReverse(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

A2

package main

import (
	"fmt"
	"strings"
)

func main() {
	// You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters.
	// You can return the answer in any order.

	// Example 1:

	// Input: s = "barfoothefoobarman", words = ["foo","bar"]
	// Output: [0,9]
	// Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
	// The output order does not matter, returning [9,0] is fine too.
	// Example 2:

	// Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
	// Output: []
	// Example 3:

	// Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
	// Output: [6,9,12]

	// Constraints:

	// 1 <= s.length <= 104
	// s consists of lower-case English letters.
	// 1 <= words.length <= 5000
	// 1 <= words[i].length <= 30
	// words[i] consists of lower-case English letters.

	StringArrayConvert("barfoothefoobarman", []string{"foo", "bar"})
	StringArrayConvert("wordgoodgoodgoodbestword", []string{"word", "good", "best", "word"})
	StringArrayConvert("barfoofoobarthefoobarman", []string{"bar", "foo", "the"})
}

func StringArrayConvert(s string, a []string) (indexs []int) {
	if 1 <= len(s) && len(s) <= 104 && 1 <= len(a) && len(a) <= 5000 {
		for _, v := range a {
			if 1 > len(v) && len(v) > 30 {
				return
			}
		}

		var tempMap []string
		for k, v := range a {
			var tempStr = v
			for key, val := range a {
				if k != key {
					tempStr = tempStr + val
				}
			}
			tempMap = append(tempMap, tempStr)
		}

		for k, v := range a {
			var tempStr = v
			for i := len(a) - 1; i >= 0; i-- {
				if k != i {
					tempStr = tempStr + a[i]
				}
			}
			tempMap = append(tempMap, tempStr)
		}

		fmt.Println(tempMap)

		for _, v := range tempMap {
			index := strings.Index(s, v)
			if index != -1 {
				indexs = append(indexs, index)
			}
		}
	}

	fmt.Println(indexs)
	return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment