Use an array of 4 bytes / char , initialize them with 0 at the start.
Every time a key is pressed , move the four bytes to the left by one ... ex value[0] = value[1]; value[1] = value[2]; value[2] = value[3];
put the read button in the last position ... value [3] = button;
optionally if the button is "C" for clear or reset , set all the 4 values in the array to 0
After every key is pressed or when a special key is pressed (an enter for example) ... do value[0] * 1000 + value[1] * 100 + value[2] * 10 + value[3] ; and you have a variable that is a number and can be stored in 2 bytes (unsigned int or something like that).
For reverse , you simply do value[3] * 1000 + value[2]*100 + value[1] * 10 + value[0] and compare it with another pre-stored variable.
You can compare that variable with a number you store in memory in the microcontroller and if they match then you unlock the thing.
This adds more cpu cycles at every key press (as you have to shift those 4 bytes every time to put the last key press in the 4th spot in the array) but saves memory since you don't have to store the password inside as a 4 character string, you can save it as a 2 byte constant number , and you also don't have to compare two strings .. you compare two numbers.
If you want to reduce memory usage even further, you can use a single 2 byte variable and use 4 bits for every key press .. 4 bits give you ability to store 0..15 in them , so you can easily store 0..9 in 4 bits.
Every time a button is pressed, you shift the value to the left by 4 bits and you add your button (0..9) to the end of the unsigned 2 byte variable.
After every change or after user presses reset , you compare the 2 byte variable to another pre-stored variable in memory.
Example ... let's say the code is 1639 ... 1 in binary as 0001 , 6 is 0110 , 3 is 0011 and 9 is 1001 so you will have to compare whatever is entered against 00010110 00111001 or 5689 in decimal (stored as unsigned integer 2 bytes.
So now yuo have a unsigned integer variable that's initially 0.
User presses "1" ... value = value << 4 + 1 === > 0 in binary is 0000 0000 0000 0000 and shifted to left by 4 bits is 0000 0000 0000 0000 and then you add 1 : 0000 0000 0000 0001 (1)
User presses "6" ... value = value << 4 + 6 === > 1 in binary is 0000 0000 0000 0001 and shifted to left by 4 bits is 0000 0000 0001 0000 and then you add 6 : 0000 0000 0001 0110 (22)
User presses "3" ... value = value << 4 + 3 === > 22 in binary is 0000 0000 0001 0110 and shifted to left by 4 bits is 0000 0001 0110 0000 and then you add 3 : 0000 0001 0110 0011 (355)
User presses "9" ... value = value << 4 + 9 === > 355 in binary is 0000 0001 0110 0011 and shifted to left by 4 bits is 0001 0110 0011 0000 and then you add 9 : 0001 0110 0011 1001 (5689)
After every digit (or when user presses ENTER) you compare the value with the value you originally stored (5689) and you can see then when user eventually presses 1639 in sequence, the value computed will be 5689 and will match with the pre-stored value.
Shifting and adding is done super fast, just a couple of cycles or something like that.. faster than moving bytes in arrays and since the variable can only store 4x4 bits at any time, it works perfectly for 4 digit codes. If you want longer pin codes, you'd have to resort to 4 byte variables (which would be able to store 8 digits) and you'd probably have to use a CLEAR or reset button to reset the variable to 0 if user makes a mistake.
With the above code that just keeps in memory the last 4 digits, if user makes a mistake he can simply start from the beginning knowing that only the last 4 button presses are memorized (for example if user pushes 8211639 and then ENTER, it would be a correct pin since only the last four numbers were memorized.
IF you want the code to work the other way around (for example if you want 9361 to also work, you just store a second value in memory (9361 using 4 bits for every digit) : 1001 0011 0110 0001 or 37,729 in decimal (unsigned 2 bytes)
and now every time a button is pushed, you shift the previously memorized value to the right by 4 bits and you store the button in the first 4 bits :
reverse_value = reverse_value >> 4;
reverse_value = reverse_value + (button << 12) - shift [0..9] to the left 12 bits and then add value to it ..