Upcoming SlideShare
×

# Amazon Interview Questions

3,884 views

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,884
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
124
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Amazon Interview Questions

1. 1. PHP: Example #1 array_unique() example<?php\$input = array("a" => "green", "red", "b" => "green", "blue", "red");\$result = array_unique(\$input);print_r(\$result);?>The above example will output:Array( [a] => green [0] => red [1] => blue)See Also  count() - Count all elements in an array, or something in an object  array_unique() - Removes duplicate values from an array  array_values() - Return all the values of an array  count_chars() - Return information about characters used in a string  Examples  Example #1 count() example  <?php \$a[0] = 1; \$a[1] = 3; \$a[2] = 5; \$result = count(\$a); // \$result == 3 \$b[0] = 7; \$b[5] = 9; \$b[10] = 11; \$result = count(\$b); // \$result == 3 \$result = count(null); // \$result == 0 \$result = count(false); // \$result == 1 ?>  Example #2 Recursive count() example  <?php \$food = array(fruits => array(orange, banana, apple), veggie => array(carrot, collard, pea)); // recursive count echo count(\$food, COUNT_RECURSIVE); // output 8
2. 2. // normal count echo count(\$food); // output 2 ?>hi to alliam using vb6.0 can any one tell me how to duplicate values in a array if possible i want to delete theduplicate valuesits urgentyes, there are many ways and methods, some faster than others. The worst, slower, but easier willbe to check each one vs each one, lets say your array is Arr1 1. dim i, j, n as integer 2. dim list() as integer 3. n=0 4. for i = lbound(arr1) to ubound(arr1) - 1 5. for j = i + 1 to ubound(arr1) 6. if arr1(i) = arr1(j) then 7. here use "home made function" to delete the j- th item from the array 8. j=j-1 9. end if 10. next j 11. next ihome made function: 1. arr1(j) = arr1(ubound(arr1)) 2. redim preserve arr1(lbound(arr1) to&nbs p;ubound(arr1) -1) OverviewArrays are a very useful and easy way of storing variables - and theyre especially easy touse in VBScript. This is due to several factors: VBScript is particularly liberal with any variable definition - that means that there is no strict defining of variables to a particular data type. The data type is assigned automatically when the variable is loaded with a value. It is even possible to mix data types within the same array. It is also possible to define the arrays in different ways: o Create the array element by element.
3. 3. o Use the VBScript Array method. o Use the VBScript Split method. o It is even possible to create multi-dimensional arrays and to make them dynamic rather than static.But, VBScript falls short, compared to other programming languages, when it comes toproviding tools for manipulating arrays. Some common array operations that VBScript doesnot provide methods for are: Adding a new elements to an array. Appending one array to the end of another array. Inserting new elements at given positions in an array. Removing elements from an array. Removing duplicate items from an array. Sorting the elements in an array. Reverse the order of the elements in an array.For some of these tasks, RhinoScript does provide useful methods. CullDuplicateNumbers - Remove duplicates from an array of numbers. CullDuplicatePoints - Remove duplicates from an array of 3-D points. CullDuplicateStrings - Remove duplicates from an array of strings. SortNumbers - Sorts an array of numbers. SortPoints - Sorts an array of 3-D points. SortStrings - Sorts an array of strings.For the other tasks, you will need to write your ownprocedures. Fortunately, VBScript provides us enough tools to write our own procedures todo most of what we would ever want to do with an array. Note, the following examples aregeneral purpose utilities. If you need something more specific, these examples are agoodstarting point. Examples Add a new element to the end of an array Sub ArrayAdd(ByRef arr, ByVal val) Dim ub If IsArray(arr) Then On Error Resume Next ub = UBound(arr) If Err.Number <> 0 Then ub = -1 ReDim Preserve arr(ub + 1) arr(UBound(arr)) = val End If End Sub Append another array to the end of an array
4. 4. Sub ArrayAppend(ByRef arr, ByVal arr0) Dim i, ub If IsArray(arr) And IsArray(arr0) Then On Error Resume Next ub = UBound(arr) If Err.Number <> 0 Then ub = -1 ReDim Preserve arr(ub + UBound(arr0) + 1) For i = 0 To UBound(arr0) arr(ub + 1 + i) = arr0(i) Next End If End SubThis example uses the .NET ArrayList object: Sub ArrayAppend(ByRef arr, ByVal arr0) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next For i = 0 To UBound(arr0) Call list.Add(arr0(i)) Next arr = list.ToArray() End If End Sub Insert a new element at a given position in an array Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val) Dim i If IsArray(arr) Then If pos > UBound(arr) Then Call ArrayAdd(arr, val) ElseIf pos >= 0 Then ReDim Preserve arr(UBound(arr) + 1) For i = UBound(arr) To pos + 1 Step -1 arr(i) = arr(i - 1) Next arr(pos) = val End If End If End SubThis example uses the .NET ArrayList object: Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr)
5. 5. Call list.Add(arr(i)) Next Call list.Insert(pos, val) arr = list.ToArray() End If End Sub Remove an element from the end of an array Sub ArrayRemove(ByRef arr) If IsArray(arr) Then If UBound(arr) > -1 Then ReDim Preserve arr(UBound(arr) - 1) End If End If End Sub Remove an element at a given position from an array Sub ArrayRemoveAt(ByRef arr, ByVal pos) Dim i If IsArray(arr) Then If pos >= 0 And pos <= UBound(arr) Then For i = pos To UBound(arr) - 1 arr(i) = arr(i + 1) Next ReDim Preserve arr(UBound(arr) - 1) End If End If End SubThis example uses the .NET ArrayList object: Sub ArrayRemoveAt(ByRef arr, ByVal pos) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.RemoveAt(pos) arr = list.ToArray() End If End Sub Remove all instances of a value from an array Sub ArrayRemoveVal(ByRef arr, ByVal val) Dim i, j If IsArray(arr) Then i = 0 : j = -1 For i = 0 To UBound(arr)
6. 6. If arr(i) <> val Then j = j + 1 arr(j) = arr(i) End If Next ReDim Preserve arr(j) End If End Sub Remove duplicate items from an arrayThis example uses VBScripts Dictionary object: Sub ArrayCull(ByRef arr) Dim i, dict If IsArray(arr) Then Set dict = CreateObject("Scripting.Dictionary") For i = 0 To UBound(arr) If Not dict.Exists(arr(i)) Then Call dict.Add(arr(i), arr(i)) End If Next arr = dict.Items End If End SubThis example uses the .NET ArrayList object: Sub ArrayCull(ByRef arr) Dim i, list, tmp If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) If Not list.Contains(arr(i)) Then Call list.Add(arr(i)) End If Next arr = list.ToArray() End If End Sub Find the first occurance of a value in an array Function ArraySearch(ByRef arr, ByVal val) Dim i ArraySearch = -1 If IsArray(arr) Then For i = 0 To UBound(arr) If arr(i) = val Then ArraySearch = i Exit Function End If
7. 7. Next End If End Function Sort the elements in an arrayThis example uses a simple Bubble Sort algorithm: Sub ArraySort(ByRef arr) Dim i, j, tmp If IsArray(arr) Then For i = UBound(arrShort) - 1 To 0 Step -1 For j = 0 To i If arr(j) > arr(j + 1) Then tmp = arr(j + 1) arr(j + 1) = arr(j) arr(j) = tmp End If Next Next End If End SubThis example uses the .NET ArrayList object: Sub ArraySort(ByRef arr) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.Sort() arr = list.ToArray() End If End Sub Reverse the order of the elements in an array Sub ArrayReverse(ByRef arr) Dim i, ub, ub2, tmp ub = UBound(arr) ub2 = Int(ub / 2) For i = 0 To ub2 tmp = arr(i) arr(i) = arr(ub - i) arr(ub - i) = tmp Next End SubThis example uses the .NET ArrayList object:
8. 8. Sub ArrayReverse(ByRef arr) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.Reverse() arr = list.ToArray() End IfEnd Sub Verifies that an array has been "dimmed"Function IsArrayDim(ByVal arr) IsArrayDim = False If IsArray(arr) Then On Error Resume Next Call UBound(arr) If Err.Number = 0 Then IsArrayDim = True End IfEnd Function Returns the number of dimensions to an arrayPublic Function GetArrayDim(ByVal arr) Dim i If IsArray(arr) Then For i = 1 To 60 On Error Resume Next Call UBound(arr, i) If Err.Number <> 0 Then GetArrayDim = i - 1 Exit Function End If Next GetArrayDim = i Else GetArrayDim = Null End IfEnd Function Print the contents of an array Sub ArrayPrint(ByVal arr) Dim i If IsArray(arr) Then For i = 0 To UBound(arr) Call Rhino.Print("Item(" &amp; CStr(i) &amp; ") = " &amp;CStr(arr(i))) Next End If End Sub
9. 9. Re: write vb script code to delete the duplicate values in an array.Ans dim a 0 Guestwer aList=Array(5,5,5,5,12,10,15,10,125,5)# 1 Dim sNewList dim newArray b= ubound(aList) For x=0 to b If InStr(sNewList,(aList(x) & ",")) <= 0 Then sNewList = sNewList & aList(x) & "," End If Next newArray = split(sNewList,",") MsgBox sNewList Is This Answer Correct ? 18 Yes 3 No Re: write vb script code to delete the duplicate values in an array.Ans There are many ways this script can be written. 4 Jaywer One simple algo which i followed was: Praka#2 sh Script Starts Here Option Explicit Dim i, j, k, aMyArr, aNewArr( ), iNewIndex, iNewLim, bFlag bFlag = 0 aMyArr = Array("AA","BB","AA", "BB" )
10. 10. iNewIndex = UBound(aMyArr)ReDim aNewArr(0)aNewArr(0) = aMyArr(0)For i=0 To UBound(aMyArr) iNewLim = UBound(aNewArr) For j = 0 To iNewLim If aMyArr(i) = aNewArr(j) Then bFlag = 0 Exit For Else bFlag = 1 End If Next If bflag = 1 Then k=iNewLim+1 ReDim Preserve aNewArr(k) aNewArr(k)=aMyArr(i) End IfNext To display the new array...For i = 0 to UBound(aNewArr) msgbox aNewArr(i)NextErase aMyArrErase aNewArrScript EndsHerePlease let me know if it satisfies ur query.
11. 11. Is This Answer Correct ? 6 Yes 1 No Re: write vb script code to delete the duplicate values in an array.Ans aList=Array(5,5,5,5,12,10,15,10,125,5,1,1,2,3,4,wer 5,6,7,8,8,8,8,8,8,8)#3 ReDim newArray(UBound(aList)) count=1 newArray(0)=aList(0) for i=0 to UBound(aList) temp=aList(i) flag=0 for j=0 to UBound(newArray) if ( newArray(j)=temp) Then flag=1 End if Next if flag=0 then newArray(count)=temp count=count+1 End If Next
12. 12. ReDim Preserve newArray(count) for j=0 to UBound(newArray)-1 msgbox newArray(j) Next Is This Answer Correct ? 4 Yes 1 NoHello all. I have an array (SortedArray) which contains duplicates, and I would like to counteach of them for a report. I know that I could use SQL to count them, but the results comefrom 20 different SQL queries. Anyways, below is the code that I have tried. Im sure it issomething simple, but I am so tired of looking at this code that I cant even think straight. Ikind of have it, but I need for DupCount not to use v_AR_Tot to size the array, and justuse the size of the unique results.strHeading = ""strLoc = ""vLocCount = 0Dim DupCount()ReDim Preserve DupCount (v_AR_Tot,6)i=0For x=0 to UBound(SortedArray)Location = SortedArray(x,1)If strHeading <> Location ThenDupCount(i,0) = Locationi=i+1vLocCount = 1DupCount(i,1) = 1strHeading = Location
13. 13. ElsevLocCount = vLocCount + 1DupCount(i,1) = vLocCountend ifNextFor j=0 to UBound(DupCount)response.Write(DupCount(j,0) & " - " & DupCount(j,1) & "<BR>")next
14. 14. Array And Duplicate ValuesI have an excel sheet containing values in column A and Column B. See bellow fordetails.ABC_________________________________aaaalbatrosbbbbirdsccccaninedddsomethingeeeeeeotherwisedddasdblablasi have a loop that goes through the first column and if the lengthe is > 4, the routinespicksup on ly the firt three letters of column B values and add a 1. However, becauseall these values must be unique, i wrote a function that should check for duplicates.I am trying to find duplicate values in the checkDup function. However, i am havingproblems with it. I load the values in the array and the pass it to the function. if it is thesame then add 1 and check again until there are no duplicate. then that value is thenext unique value.There is a twist: the first three letters can repeat and that is fine as long as the addedinterger from the counter is different. But the counter should be reset to 1 when a newset of values are present.I need help with this. I am stuck.Code:Option ExplicitDim MyArray(60)Dim MyArray2(30)Dim myval, myval2 As StringDim i, j, count As IntegerPrivate Sub cmdAcct_Click()Dim i, j, count As IntegerDim myval, myval2 As StringRange("C1:F60").Clearj=1
15. 15. count = 0For i = 2 To 60Range("A" & i).SelectIf Len(Range("A" & i).Value) <= 4 ThenRange("D" & i).Value = Range("A" & i).ValueElseIf Len(Range("A" & i).Value) > 4 Thencount = count + 1myval2 = Left(Range("b" & i).Value, 3) & count******************** this is to check for duplicateMyArray(j) = myval2 assigns the value to the arraymyval = checkDup(MyArray(j)) then checks throught the function if it is a dupj=j+1********************Range("D" & i).Value = myvalElseIf Range("A" & i).Value = "" ThenRange("D" & i).Value = "****"End IfNext iCall checkDupRange("A1").SelectEnd SubFunction checkDup(ParamArray MyArray())Dim i As IntegerDim result As String********************************For i = 1 To UBound(MyArray)result = result & MyArray(i) & vbNewLineNext iMsgBox resultEnd Function
16. 16. var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];var sorted_arr = arr.sort(); // You can define the comparing function here. // JS by default uses a crappy string compare.var results = [];for (var i = 0; i < arr.length - 1; i++) { if (sorted_arr[i + 1] == sorted_arr[i]) { results.push(sorted_arr[i]); }}alert(results);Common questions:Q: Given two numbers m and n, write a method to return the first number r that isdivisible by both (e.g., the least common multiple).A:The Approach:What does it mean for r to be divisible by m and n? It means that all the primes in m must go into r,and all primes in n must be in r. What if m and n have primes in common?For example, if m is divisible by 3^5 and n is divisible by 3^7, what does this mean about r? It means rmust be divisible by 3^7.The Rule: For each prime p such that p^a m (e.g., m is divisible by p^a) and p^b n, r must bedivisible by p^max(a, b)The Algorithm:Define q to be 1.for each prime number p less than m and n:find the largest a and b such that p^a m and p^b nlet q = q * p^max(a, b)return qLinks: http://hpsqtp.blogspot.in/search?updated-min=2012-01-01T00:00:00%2B05:30&updated-max=2013-01-01T00:00:00%2B05:30&max-results=2http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reversing-a-string-with-vbscript-using-the-mid-function/
17. 17. http://thecareerplus.com/