id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

tmhscs@gmail.com stacks > stack_searchAndPushIndex
prev  |  next  |  chance

Given a Stack of integers and a target value, if the target value exists somewhere in the stack, push the target value's 1-based index onto the top of the stack and return the stack. The 1-based index means that if the target value is on the top of the stack already, push a 1. If it is the second element in the stack, then push a 2. If the target value does not exist in the stack, push a -1 to the top of the stack.

YOUR FIRST TWO LINES OF CODE MUST BE:
Stack<Integer> stack = new Stack<Integer>();
stack.addAll(list);

stack_searchAndPushIndex([1, 2, 3, 4, 5], 3) → [1, 2, 3, 4, 5, 3]
stack_searchAndPushIndex([1, 2, 3, 4, 5], 6) → [1, 2, 3, 4, 5, -1]
stack_searchAndPushIndex([1, 2, 3, 4, 5], 1) → [1, 2, 3, 4, 5, 5]

...Save, Compile, Run (ctrl-enter)

public Stack<Integer> stack_searchAndPushIndex(List<Integer> list, int value) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Post-solution available

Copyright Nick Parlante 2017 - privacy